0

I need to rewrite URL's using .htaccess to redirect all users from old sitemap to the new URL's. The old URLs looks like this:

http://server.linux.example.com/a/v/c/document_title1.php
http://server.linux.example.com/jp/x/1/o/document_title32.php
http://server.linux.example.com/kr/a/a/2/document_title12343.php
http://server.linux.example.com/cn/6/z/z/document_title124.php

I need to keep first directory and then remove all sub-directories, including slashes and characters (letters and numbers between /) so the new URLs will look like this:

http://server.linux.example.com/document_title1.php
http://server.linux.example.com/jp/document_title32.php
http://server.linux.example.com/kr/document_title12343.php
http://server.linux.example.com/cn/document_title124.php

This is what I made so far using help from stackoverflow.com.

(?<=http://server\.linux\.example\.com/)(jp/|kr/|cn/)?.*(?=\.php)

regex101.com shows that this is a valid regex but I'm having problems with implementing this within the .htaccess as I'm reciving 500 erros.

ReWriteRule (?<=http://server\.linux\.example\.com/)(jp/|kr/|cn/)?.*(?=\.php)

What I'm doing wrong?

2 Answers 2

1

I'd chose an approach like this:

RewriteEngine on 
RewriteRule ^/?([^/]+)/.+/([^/]+\.php)$ /$1/$2 [END]

You may need to add some exceptions depending on your specific situation, but in general it should do what you want.

That rule should work likewise in dynamic configuration files (".htaccess") and in the real http servers host configurations.

And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Sign up to request clarification or add additional context in comments.

12 Comments

I just tested your solution and unfortunately it gives me 500 error :(
Hm, then you need to check your http servers error log file to find out what the actual issue is.
A wild shot, but might be the issue here: maybe you operate a very old, outdated version of the apache http server which does not yet know the [END] flag. Have a try replacing it by the [L] flag.
I rechecked, just to be certain since I did not actually test the rule before I posted it. But the check is fine, the rule is valid and working as expected.
And with ^ ?: RewriteRule ^/?([^/]+)/.+/([^/]+\.php)$ /$1/$2 [L]
|
0

Try with:

RewriteEngine on 
RewriteRule ^/?(jp/|kr/|cn/)?.+/([^/]+\.php)$ /$1$2 [L]

You can redirect (and not only rewrite) with [R=302] instead of [L].

And to find a problem, look at your error logs.

1 Comment

RewriteEngine on RewriteRule ^/?(jp/|kr/|cn/)?.+/([^/]+\.php)$ /$1$2 [R=302] did the trick on Apache/2.4.25 (Debian). Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.