I am having trouble with setting up a redirect. I have index pages in certain directories, the directory names are simple six character long tokens, like ABCDEF. Names are alphanumeric and all capital letters. What I originally wanted was to direct all traffic, lower case and upper case that contains the token to the correct directories. I don't have access to server modules, like mod_speling, therefore I use a php script to do the actual url rewrite. It works. But then I wanted to add one more step, to direct all traffic like:
example.com/ABCDEF
example.com/abcdef
example.com/reference/abcDEF
example.com/referENces/ABCdeF
to the directory:
example.com/references/ABCDEF/
I couldn't get these work, my redirect gets in a loop all the time.
What I have in my htaccess:
# takes care of: example.com/abcdef
RewriteCond %{DOCUMENT_ROOT}/references/%{REQUEST_URI} !-d
RewriteCond %{REQUEST_URI} ^(/?)([a-zA-Z0-9]{6})(/?)$
RewriteRule ^(/?)([a-zA-Z0-9]{6})(/?)$ example.com/rewrite.php?rewrite=example.com%{REQUEST_URI} [QSA,L,NC,R=302]
# takes care of example.com/reference/abcDEF
RewriteCond %{REQUEST_URI} ^(/?)([REFNCS?refncs?]{9})/([a-zA-Z0-9]{6})(/?)$
RewriteRule ^(/?)([REFNCS?refncs?]{9})/([a-zA-Z0-9]{6})(/?)$ example.com/rewrite.php?rewrite=example.com%{REQUEST_URI} [QSA,L,NC,R=302]
# this should take care of: example.com/references/abcDEF , but if I uncomment it, we end up in a loop
#RewriteCond %{REQUEST_URI} ([REFNCSrefncs]{10})/([a-zA-Z0-9]{6})(/?)$
#RewriteRule ([REFNCSrefncs]{10})/([a-zA-Z0-9]{6})(/?)$ example.com/rewrite.php?rewrite=example.com%{REQUEST_URI} [QSA,L,NC,R=302]
# Finally this handles: example.com/ABCDEF
RewriteCond %{DOCUMENT_ROOT}/references/%{REQUEST_URI} -d
RewriteCond %{REQUEST_URI} ^(/?)([a-zA-Z0-9]{6})(/?)$
RewriteRule ^(/?)([a-zA-Z0-9]{6})(/?)$ example.com/references%{REQUEST_URI} [QSA,L,NC,R=302]
At this point I am sure there must be a better, more general regexp solution. The second and third block could be merged somehow.
Peter
Try changing:
RewriteCond %{REQUEST_URI} ([REFNCSrefncs]{10})/([a-zA-Z0-9]{6})(/?)$
RewriteRule ([REFNCSrefncs]{10})/([a-zA-Z0-9]{6})(/?)$ example.com/rewrite.php?rewrite=example.com%{REQUEST_URI} [QSA,L,NC,R=302]
To:
RewriteCond %{REQUEST_URI} ^(\/?)([REFNCSrefncs]{10})\/([a-zA-Z0-9]{6})(\/?)$
RewriteRule ^(\/?)([REFNCSrefncs]{10})\/([a-zA-Z0-9]{6})(\/?)$ example.com/rewrite.php?rewrite=example.com%{REQUEST_URI} [QSA,L,NC,R=302]
Looks like this works:
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-d
RewriteCond %{REQUEST_URI} ^(/?)([REFNCSrefncs]{10})/([a-zA-Z0-9]{6})(/?)$
RewriteRule ^(.*)$ example.com/rewrite.php?rewrite=example.com%{REQUEST_URI} [QSA,L,NC,R=302]
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-d
RewriteCond %{REQUEST_URI} ^(/?)([REFNCrefnc]{9})/([a-zA-Z0-9]{6})(/?)$
RewriteRule ^(.*)$ example.com/rewrite.php?rewrite=example.com%{REQUEST_URI} [QSA,L,NC,R=302]
I had to change the order of ([REFNCSrefncs]{10}) and ([REFNCrefnc]{9}).
Thanks for the hints!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.