0

I have searched and searched for an answer to this, but none of the posts I've found on stackoverflow work for me - nor do the online htaccess generators.

I have made slight adjustments to Anubhava's excellent answer on htaccess redirect for dynamic urls not working to suit different page names as follows:

RewriteEngine On

# for external redirection from `/hp.php?su=sitename` to `/sitename`
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+hp\.php\?su=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]

# for internal redirection from `/sitename` to `/hp.php?su=sitename`
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /hp.php?su=$1 [L,QSA]

However, in addition to sending users to domain.com/sitename (which the modified code above does) I also want to change the following urls.

domain.com/newpage.php?su=sitename&PgID=1234&pu=pagename

to become

domain.com/sitename/1234/pagename.html

similary

domain.com/diary.php?su=sitename

to become

domain.com/sitename/diary.html

This last one would be replicated for similar dynamic pages, such as

future.php?su=sitename >> domain.com/sitename/future.html
photos.php?su=sitename >> domain.com/sitename/photos.html

etc

Some time in the future, I would also like to divert http:// to https:// - would this rule go before all the others?

Hope somebody can help

UPDATE: RewriteEngine On

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/hp\.php\?su=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/newpage\.php\?su=([^\s&]+)&PgID=(\d+)&pu=([^&\s]+) [NC]
RewriteRule ^ /%1/%2/%3.html? [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/(diary|future|photos)\.php\?su=([^\s&]+) [NC]
RewriteRule ^ /%2/%1.html? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /hp.php?su=$1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(diary|future|photos).html$ /$2.php?su=$1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(\d+)/(.+)\.html$ /newpage.php?su=$1&PgID=$2&pu=$3 [L,QSA]
Community
  • 1
  • 1
Chris
  • 37
  • 1
  • 7

1 Answers1

0

It isn't all that difficult to derive from the previous rule:

RewriteEngine On

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/hp\.php\?su=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/newpage\.php\?su=([^\s&]+)&PgID=(\d+)&pu=([^&\s]+) [NC]
RewriteRule ^ /%1/%2/%3.html? [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/(diary|future|photos)\.php\?su=([^\s&]+) [NC]
RewriteRule ^ /%2/%1.html? [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /hp.php?su=$1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(diary|future|photos).html$ /$2.php?su=$1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(\d+)/(.+)\.html$ /newpage.php?su=$1&pgID=$2&pu=$3 [L,QSA]

As to your second question about http to https, yes; that particular redirection should occur before all others.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
  • Thanks @hjpotter92 - but unfortunately, the one I've tested doesn't work; domain.com/newpage.php?su=abc&PgID=5&pu=xyz doesn't go anywhere and the url stays in the address bar. (Sorry if you were notified before this reply was complete - didn't know return posted the reply!) – Chris Oct 08 '15 at 16:41
  • @Chris If you have access to the apache's access_logs file, please provide the logged request? – hjpotter92 Oct 08 '15 at 16:46
  • Hi ajpotter92 - unfortunately I don't have access to these. – Chris Oct 08 '15 at 16:59
  • @Chris The order of rules is very important here. Also, clear your browser caches before testing the rules. I'll update the last 3 rules too, just in case. – hjpotter92 Oct 08 '15 at 17:09
  • @ajpotter92 - hmmn! Done that and it still doesn't work - and the starting one no longer works either - domain.com/sitename. Many thanks – Chris Oct 08 '15 at 17:39
  • @Chris Does a directory named `sitename` exist? Remove the 3 lines after (and including) this one: `RewriteCond %{REQUEST_FILENAME} -f [OR]` and test again – hjpotter92 Oct 08 '15 at 18:08
  • @ajpotter92 - my mistake (and my apologies) - the long url redirect does work, but the domain.com/sitename one no longer works. I have removed the 3 lines suggested. I don't have enough points to move this to chat -if that would be easier. Thanks again EDIT - to be more specific, it does redirect, but I get an 'internal server error' message, whereas before the content of the page was displayed – Chris Oct 08 '15 at 18:58
  • @Chris Can you put the current htaccess file in your question above? – hjpotter92 Oct 08 '15 at 19:08