logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Apache View previous topic :: View next topic
Reply to topic   Topic: htaccess redirect after rewrite rule
Author
tdkappa



Joined: 15 Feb 2017
Posts: 8

PostPosted: Mon 30 Jul '18 9:09    Post subject: htaccess redirect after rewrite rule Reply with quote

I have my .htaccess written like this:

Code:

RewriteEngine On
RewriteRule  ^([0-9]+)/([a-zA-Z0-9-]+)/$ /?p=$1


and the address like https://mysite.com/162/title-of-my-post-id162/ are correctly solved to https://www.mysite.com/?p=162

now I have a list of public URLs to redirect to other address, for example:

https://mysite.com/200/title-of-post-with-id-200/ redirect to > https://mysite.com/100/title-of-my-post-with-id-100/

or

https://mysite.com/201/title-of-post-with-id-201/ redirect to > https://mysite.com/158/title-of-my-post-with-id-158/

and so on..

I try with:

Code:

Redirect /201/title-of-post-with-id-201/ /158/title-of-my-post-with-id-158/


but don't work well, beacause in the browser address bar, I see:

https://mysitedotcom/158/title-of-my-post-with-id-158/?p=201

and I don't want't to see ?p=201

How Can I resolve it?

By htaccess configuration or by php script?! Crying or Very sad Crying or Very sad
Back to top
mraddi



Joined: 27 Jun 2016
Posts: 152
Location: Schömberg, Baden-Württemberg, Germany

PostPosted: Mon 06 Aug '18 15:33    Post subject: Reply with quote

Hello,

please don't mix mod_alias (for Redirect) and mod_rewrite (for RewriteRule ...) as mod_rewrite is processed before the mod_alias. See http://web.archive.org/web/20131107001958/http://my.opera.com/GreyWyvern/blog/2007/09/12/apache-mod-rewrite - paragraph "Order of Execution"

So the best way would be (as mentioned in the provided link) to not use mod_alias and replace these rules by mod_rewrite-rules and place the static-redirect-urls-to-other-urls-rules with the flag "L" before the already existing RewriteRule in your .htaccess.

So you may try to use this code as example/base for further engineering?
Code:
RewriteEngine On

RewriteRule  ^/200/title-of-post-with-id-200              /100/title-of-my-post-with-id-100            [R=301,L]
RewriteRule  ^/201/title-of-post-with-id-201              /158/title-of-my-post-with-id-158            [R=301,L]

RewriteRule  ^([0-9]+)/([a-zA-Z0-9-]+)/$ /?p=$1   [L]
Back to top
tdkappa



Joined: 15 Feb 2017
Posts: 8

PostPosted: Tue 07 Aug '18 9:43    Post subject: Reply with quote

hello mraddi

I try with Your example, but RewriteRule for the static address doesn't work

I see the content of the original address

For example:

Code:

RewriteRule  ^/200/title-of-post-with-id-200              /100/title-of-my-post-with-id-100            [R=301,L]


I see the content of ../200/title-of-post-with-id-200/
Back to top
tdkappa



Joined: 15 Feb 2017
Posts: 8

PostPosted: Tue 07 Aug '18 9:50    Post subject: Reply with quote

Ok, now it works !!
there is a problem with the final slash in the url !!

the correct Rewrite is:

Code:

RewriteRule  ^/200/title-of-post-with-id-200/              /100/title-of-my-post-with-id-100/            [R=301,L]


Thank You a lot !!
Back to top


Reply to topic   Topic: htaccess redirect after rewrite rule View previous topic :: View next topic
Post new topic   Forum Index -> Apache