htaccess multiple parameter values not working properly - regex

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.

Related

.htaccess redirect with GET parameters and URL condition

I try to redirect to another domain and changing the URL structure (not keeping URI part) based on 2 conditions:
The original URL has "lang" parameter.
The original URI begins with "page".
It's Apache 2.2
This is working when only have the lang paramenter in the orginal URL:
RewriteCond %{QUERY_STRING} lang=en$
RewriteRule ^.*$ https://www.destination.com/en/? [R=301,L]
For example it redirects ok for:
http://www.origin.com/?lang=en
to:
http://www.destination.com/en/
But I also need to redirect something like:
http://www.origin.com/page/5/?lang=en
to:
http://www.destination.com/en/
I'm trying things like this, but doesn't work, I think there's something wrong in the RewriteRule pattern:
RewriteCond %{QUERY_STRING} ^lang=en$
RewriteRule ^/page/.*/?$ https://www.destination.com/en/? [R=301,L]
Welcome!
You may not want to do have a ? at the end of your RewriteRule. However, if you wish to have you can do so. Maybe, you want to have a RewriteRule similar to:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} lang=en$
RewriteRule (.*)(\?lang=en) https://www.destination.com/en/ [R=301,L]
</IfModule>
You might want to restart your apache, with a command similar to:
sudo apachectl restart
You might clear your browser cache every time that you make a change on .htaccess file
Graph
This graph shows how your expression works, if you wish to know:
This tool can help you to simply design any rule that you like to have.
I have assumed that you want to redirect all your ?lang=en containing ULRs to a single URL. I was not sure about that. If that's not the case, you may not use my expression.

Htaccess Regex won't match

I'm in desperate need of a quick tip.
Trying to use htaccess to change this not so lovely url
http://localhost/test/index.php?page=Article&articleID=61
to
http://localhost/test/article/2015-09-21-this-is-the-headline
From what I've gathered I need to send the last part to a php script which can then get the matching id from the database. Knowing that I should be able to send the user to the original url up top.
RewriteRule ^(.*)\/article\/(.*)$ redirect/article.php [L]
# RewriteRule ^(.*)$ index.php
As of right now I'm not passing the information to the script yet. redirect/article.php only contains a print statement to let me know once I get that far.
However, despite my brain and every regex debugger saying otherwise, it won't match the url provided in the second code box. All I'm getting is the good old 404. If I activate the second rule it is applied to my url, telling me that the first one is simply being skipped.
What am I missing?
.htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
# rename individual pages
RewriteRule ^(.*)\/article\/(.*)$ redirect/article.php [L]
# RewriteRule ^(.*)$ index.php
# resize images
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.)*\/([0-9]+)\-(([0-9]|[a-z])+)\.(prev)$ filePreview.php?id=$2&size=$3 [L]
php_value upload_max_filesize 20M
php_value post_max_size 21M
</IfModule>
The location of a .htaccess file informs how you must list paths for mod_rewrite. Inside .htaccess, paths for RewriteRule are not received with a leading /. Since yours was residing in /test, the (.*) at the start of your rule wasn't matching anything and harmless. Since that was followed by /, the article/ path was expecting a / it would never receive. The simplest fix is to change this rule to match article at the start via:
RewriteRule ^article/(.*) redirect/article.php [L]
Assuming you'll use that as a lookup in the PHP script, add a parameter to use the $1 captured pattern like:
RewriteRule ^article/(.*) redirect/article.php?article=$1 [L]

mod_rewrite: Redirect all urls ending with "/video/[anyfilename].mp4"

Sorry for this probably very noob-style question, but I just can't get it to work.
Here's my current .htacces file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^_res/.*$ - [L] # all URLs beginning with "_res" are not changed.
# put new fancy rule doing what I want here...
RewriteRule . index.php [L] # all other URLs are redirected to index.php
</IfModule>
This works fine. It's redirecting all urls except the ones starting with "_res/" to index.php.
What I need now is that all URLs matching "[anypath]/video/[anyfilename].mp4" will be rewritten to "content/[anypath]/video/[anyfilename].mp4" (which is the actual path of this file on the server - I can't use x-sendfile on this shared web space, thus I need to rewrite large file urls to their actual server locations to avoid php's fileread-function).
From my understanding of these RewriteRules, I think I have to place this rule just before the last one.
Unfortunately my regexp-expertise is practically non-existent.
What I thought should work is this:
RewriteRule ^(.*)/video/(^/*)\.mp4$ content/$1/video/$2.mp4 [L]
The regexp should mean "starts with any amount of any characters, followed by "/video/", followed by any amount of any characters which are not '/' and ends with ".mp4".
When I insert this into my .htaccess right before the last rule, urls ending with "/video/myvid.mp4" are still rewritten to index.php.
You see me clueless. Any suggestions?
Thanks alot in advance!
I think you want:
RewriteCond %{REQUEST_URI} !^/content/
RewriteRule ^(.*)/video/([^/]+)\.mp4$ content/$1/video/$2.mp4 [L]
And you want to add this rule above the one that routes to index.php

How to redirect from an old php script to a new php script using mod_rewrite

It's my first request here and hopefully I won't upset anyone.
Here's my story:
I have looked all over this place for a solution and wasn't able to find one. Here's hoping that someone can give some input.
I've basically managed to use Apache's mod_rewrite to create SEO-Friendly urls for my website.
E.g. Old path www.hostname/index1.php?session=user is now rewritten as www.hostname/user, which results into a SEO Friendly URL address.
However, the old path is still valid. I need to somehow redirect all the incoming old index1.php requests to the newer URLs, the SEO-Friendly ones, for the search engines to transfer the link popularities to the new ones. I believe I may have an infinite-loop redirect and that's why it's not working.
My code so far:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
## Redirect still valid old links to SEO friendly ones
RewriteRule ^/?index1\.php\?session=user$ /user [R=301]
## Catch the above and rewrite the URL
RewriteRule ^/?user/?$ /index1.php?session=user [QSA,L]
The above rules never get hit when the htaccess file is parsed.
It hit me that I might be doing some sort of redirect loop here so I thought about renaming the index1.php file to index2.php and create something like:
## Redirect still valid old links to SEO friendly ones
RewriteRule ^/?index1\.php\?session=user$ /user [R=301]
## Catch the above and rewrite the URL
RewriteRule ^/?user/?$ /index2.php?session=user [QSA,L]
However, that failed too.
What would be the best approach to this? What am I doing wrong here?
Thank you!
Update your .htaccess rules to
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
## Redirect still valid old links to SEO friendly ones
RewriteCond %{QUERY_STRING} !no-redir [NC]
RewriteCond %{QUERY_STRING} session=user [NC]
RewriteRule ^/?index1\.php$ /user? [R=301,NC,L]
## Catch the above and rewrite the URL
RewriteRule ^/?user/?$ /index1.php?session=user&no-redir [QSA,NC,L]
You can't match against the query string (everything after the ?) in a rewrite rule, so you can't match against the session= part. You also can't simply match against the %{QUERY_STRING} var because that gets populated by you other rule when it rewrites the SEO friendly URL to the one with the query string. So you need to match against the actual request:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /index1\.php\?session=([^&]+)&?([^\ ]*)
RewriteRule ^ /%2?%3 [L,R=301]

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.