.htaccess shorten middle part of url, keep string after last slash - regex

I'm trying to write a mod_rewrite rule to do the following:
Request URL:
https://dev.website.com/user/name
to work as
https://dev.website.com/foo/bar/user/name
This is my current .htaccess mod_rewrite, but it's not working.
Regex tools are matching the string, but apache isn't.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^.*\/user\/(.+)*$ ./foo/bar/user/$1

revo's answer in this comment worked.
RewriteRule ^.*/(user/[^/]*)/?$ /foo/bar/$1 [L]

Related

htaccess multiple parameter values not working properly

I want to get this URL:
www.example.com/scooter-details/1/vespa-sprint-matt-midnight-blue
But the URL I get is:
www.example.nl/scooter-details/1/Vespa/-Sprint%20-%20Matt%20Midnight%20Blue
Question
How can I delete all the white spaces %20 and replace it with hyphen characters (-). Also, is it possible to change the QUERY STRING Vespa/-Sprint%20-%20Matt%20Midnight%20Blue to lowercase vespa/-sprint-matt%20Midnight%20Blue
Here is the htaccess code
Options +FollowSymLinks -MultiViews
RewriteEngine On
# SEO FRIENDLY URL
# Redirect "/scooter-details.php?scooter_id=<num>&scooter_brand<brand>&scooter_model<model>" to "/scooter-details/<num>/<brand>-<model>"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^scooter_id=(\d+)&scooter_brand=([^/.]+)&scooter_model=([^.]+)$
RewriteRule ^(scooter-details)\.php$ /$1/%1/%2/$2-%3$3 [QSD,L,R=301,NE]
# Rewrite /scooter-details/<num>/<brand>-<model>" back to "/scooter-details.php?scooter_id=<num>&scooter_brand<brand>&scooter_model<model>"
RewriteRule ^(scooter-details)/(\d+)/([^/]+)$ $1.php?scooter_id=$2&scooter_brand=$3&scooter_model=$4 [L]
You should implement that redirect in your PHP code rather than in .htaccess. That type of processing is hard to do in .htaccess, but easy to implement in PHP. You should remove your QSD rewrite rule and its conditions.
So that your PHP script can tell when it should redirect vs when it shouldn't, pass an additional parameter in your last rewrite rule:
# Rewrite /scooter-details/<num>/<brand>-<model>" back to "/scooter-details.php?scooter_id=<num>&scooter_brand<brand>&scooter_model<model>"
RewriteRule ^(scooter-details)/(\d+)/([^/]+)$ $1.php?redirect=no&scooter_id=$2&scooter_brand=$3&scooter_model=$4 [L]
If your PHP script sees the redirect=no parameter, it should produce the page. If that parameter is missing, it should build the canonical URL and redirect to it.

URL rewriting to a folder if url has one pattern and ignore the rule if url has another pattern

I have following directory structure for the project
example
admin/
cdn/
client/
.htaccess
When the url is
http://192.168.1.121/example/ or
http://192.168.1.121/example or
http://192.168.1.121/example/category/productname or
http://192.168.1.121/example/content/12 and so on,
it should rewrite to client/ folder.
But if url has http://192.168.1.121/example/cdn, it should ignore the rule.
Can anybody help me on this. How to write the rule in .htaccess
Thanks
You can use this /example/.htaccess:
RewriteEngine On
RewriteBase /example/
RewriteRule ^(?!client/|cdn)(.*)$ client/$1 [L,NC]
(?!client/|cdn) is negative lookahead to ignore the rule when URI is /example/cdn OR /example/client
Something like that
RewriteRule ^/example/cdn . [L] Do nothing if it starts with "/example/cdn" and stop there
RewriteRule ^/example /client$0 [L] Prepend "/client" if it starts with "/example"

redirecting URL with .htaccess not working

Trying to do a simple URL rewrite with .htaccess but it does not seem to be working.
Want to access http://www.example.com/shop/index.php?route=information/information&information_id=11
using the URL http://www.example.com/MyGreatClub2013
This is what I have in my .htaccess which is stored at the www.example.com root level
RewriteEngine On
RewriteRule ^/MyGreatClub2013$ /shop/index.php?route=information/information&information_id=11 [NC,L]
Am I doing something stupidly wrong?
Remove leading slash:
RewriteEngine On
RewriteRule ^MyGreatClub2013/?$ /shop/index.php?route=information/information&information_id=11 [NC,L,QSA]
mod_rewrite rules when used in .htaccess don't match leading forward slash as .htaccess is per directory.
Here's a decent guide: http://www.javascriptkit.com/howto/htaccess.shtml
Hope it helps.

Regex htaccess rewrite to strip url prefix

So many questions come close to it, but not quite there, and I'm afraid regex makes my head spin!
After a site recode, which slightly amended some urls - which we don't want to go 404 in Google, I am looking for a 301 redirect to remap:
http://www.oursite.com/listing-product-name-1234.html
to
http://www.oursite.com/product-name-1234.html
...you can see we just want to drop "listing-". The "1234" is a unique ID per product, and the product name can be multiple words (hyphen separated) by the way.
Thanks very much for the help regex heads!
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^listing-(.+?\.html)$ /$1 [L,R=301,NC]
Try this:
RewriteCond %{REQUEST_URI} ^/listing-(\w+-)+\d+\.html
RewriteRule ^listing-(.*) $1

Can't get mod_rewrite to work when only having to rewrite certain urls

I have the following Rules setup in my .htaccess file
RewriteRule ^detail.php / [R=301,NC,L]
RewriteRule ^tempate.php / [R=301,NC,L]
What these do is perform a 301 redirect when detail.php and template.php is called, although this works this is not working when I enter mydomain.co.za/detail.php?product_id=2432&category=700 then it redirects the website to mydomain.co.za/?product_id=2432&category=700
I need any url where the filename is detail.php or template.php with any amount of parameters in the query string to redirect to the home page
I tried RewriteRule ^detail.php?(.*) / [R=301,NC,L] and this also not working. Any help or guide will be appreciated.
Replace your 2 RewriteRule lines with this line:
RewriteRule ^(?:detail|template)\.php$ /? [R=301,NC,L]
Note that question mark after /, that is a special mod_rewrite syntax to strip out any existing query string from original URI.
Also remember that RewriteRule only matches URI without query string therefore your attempt of ^detail.php?(.*) / [R=301,NC,L] won't work as you expected.