1

Right now i am using the given below regex

RewriteRule ^(love-wallpaper.php|quotes-wallpaper.php)+ wallpapers.php

to rewrite love-wallpaper.php and quotes-wallpaper.php to wallpapers.php

I want above rule should not rewrite wrong url like love-wallpaper.php&anything
but should rewrite to url like love-wallpaper.php?anything to wallpapers.php

How can this be done?

Example :
Urls like love-wallpaper.php#asdfasdfasdf should not rewrite to wallpapers.php
but url like love-wallpaper.php?aasdbfsbdf should redirect to wallpapers.php
Thanks

Tarun
  • 3,162
  • 3
  • 29
  • 45
  • FYI, your regex would also match `love-wallpaperXphp`, as `.` means 'äny character'. Use `\.` for a regular dot character. Secondly, why are you using `+` which means repeated 1 or more times? Do you want to accept `live-wallpaper.phplove-wallpaper.phplove-wallpaper.php` as an URL? – h2ooooooo Feb 05 '13 at 08:14
  • Thank you h20,I dnt know much about regex. – Tarun Feb 05 '13 at 08:16
  • @C1D , edit the question can you now provide any help – Tarun Feb 05 '13 at 08:25
  • @h2ooooooo No i dnt want to accept that kind of url ,what should i use in place of "+" – Tarun Feb 05 '13 at 08:28

2 Answers2

1

It's not very clear to me what you are looking for, But If you want to match for a valid query string after quotes-wallpaper.php use below rule

RewriteRule ^(love-wallpaper.php|quotes-wallpaper.php)+(?![\w&#\$!\(\)]+) wallpapers.php

It will not match &,#,$ etc after wallpaper.php.You can put more symbols in regex character class[] if you don't want them to match and rewrite and vice-versa.

Note: This will entirely ignore any query string in passed in url.

Varinder Singh
  • 1,570
  • 1
  • 11
  • 25
  • http://www.example.com/celebs-wallpaper.php&ab ,This url is still rewritting to wallpapers.php though its invalid – Tarun Feb 05 '13 at 08:15
  • @MichelFeldheim yes `(?![\w\$!\(\)]+)` is not looking for query string anywhere.It's filtering url that don't have a querystring after `.php` part.If starting charcter is not `?` then it's not a valuid query string and your point is not valid – Varinder Singh Feb 05 '13 at 09:10
  • I believe `RewriteRule ^(love-wallpaper.php|quotes-wallpaper.php)+(?![\w\$!\(\)]+) wallpapers.php` should work as expected. – Varinder Singh Feb 05 '13 at 09:16
1

You're probably looking for the following:

RewriteRule ^(love|quotes)\-wallpaper\.php$ wallpapers.php [QSA]

I've also escaped the - character, as it's reserved in regex.

Explanation of regex/htaccess:

  • ^ - "starts with"
  • (love|quotes) - the string "love" or the string "quotes"
  • \-wallpaper\.php - the string "-wallpaper.php" with - and . escaped.
  • $ - "end must be here"
  • [QSA] sends your URL parameters (url.php?my=parameter) onto the wallpapers.php page.

You cannot decide not to match url.php#hash as URL hashes cannot be used in .htaccess matching, as they're never sent to the server - see Redirect URL with hash using .htaccess file

Community
  • 1
  • 1
h2ooooooo
  • 39,111
  • 8
  • 68
  • 102