url redirect from _ to - htaccess - php

I have problem in my URL.
This is my code in htaccess:
RewriteRule ^music-(.*)-([0-9_]+)\.html$ /artiste.php?g=$1&page=$2 [NC,L]
So some URL on Google or Bing could be showing like this music_(.*)_([0-9_]+)\.html
If possible I want to change _ to - with htaccess.
I want any url with _ to change to -, because all links work correct with - but in my research some URLs have _ so I want to replace them with -.
example:
Error : http://www.example.com/me_like_this.html
Correct : http://www.example.com/me-like-this.html

You can use the following in your /.htaccess file:
RewriteEngine On
# Replace underscores with hyphens, set the environment variable,
# and restart the rewriting process. This essentially loops
# until all underscores have been converted to hyphens.
RewriteRule ^([^_]*)_(.*)$ $1-$2 [E=underscore:yes,N]
# Then, once that is done, check if the underscore variable has
# been set and, if so, redirect to the new URI. This process ensures
# that the URI is rewritten in a loop *internally* so as to avoid
# multiple browser redirects.
RewriteCond %{ENV:underscore} yes
RewriteRule (.*) /$1 [R=302,L]
Then add your rule afterwards:
RewriteRule ^music-(.+)-(\d+).html$ /artiste.php?g=$1&page=$2 [NC,L]
If this is working for you, and you would like to make the redirects cached by browsers and search engines, change 302 to 301.
Note: In your RewriteRule I have changed .* to .+ so it only matches one or more characters, instead of zero or more characters. Additionally, I have changed [0-9_]+ to \d+, which is the shorthand equivalent without including underscores, which would be converted to hyphens anyway. If you want to include hyphens in the last capture group, then change (\d+) to ([\d-]+).

RewriteEngine On
RewriteRule ^(.*)_(.*)$ /$1-$2 [L,R=301]
RewriteRule ^music-(.*)-([0-9_]+)\.html$ /artiste.php?g=$1&page=$2 [NC,L]
Please try this.

Related

.htaccess redirect urls with spaces

I am trying to redirect all invalid urls to my index.php file via my .htaccess file.
Unfortunately I keep getting an Apache error.
My .htaccess file
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^([a-zA-Z0-9\-\_\/]*)$ index.php?p=$1
RewriteRule ^([A-Za-z0-9\s]+)$ index.php?p=$1 [L] 
This invalid url shoud redirect to index.php:
/vacatures/jobapplication/facility-manager%20qsdf
But it throws the object not found 404 Apache error.
The rule you have which allows spaces does not allow hyphens. The rule you have which allows hyphens does not allow spaces. So anything which includes both will not match either.
Your invalid URL facility-manager%20qsdf includes both.
My guess is that your RewriteCond is supposed to apply to both rules, but that is not what is happening now, it will apply only to the first RewriteRule after it. You can solve all these problems by including just 1 RewriteRule, and amending it to accept everything you want:
RewriteRule "^([A-Za-z0-9\-\_\/\s]+)$" index.php?p=$1 [L]
Note that this requires at least one of the characters in your character class, in other words it will not match your "home" location when there is no path ("http://somewhere.com/"). If you want to also match for that location, change the + to a *, to allow 0 or more character matches.
Your rewrite rules do not match the url you indicated. Your REQUEST_URI is
/vacatures/jobapplication/facility-manager%20qsdf
I suspect the URL decoding is not done before the RewriteRule matching and therefore it's trying to match literally %20, yet % sign is not included in your match. I'm not sure why you're using two RewriteRules - why not do something like this?
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteCond %{REQUEST_URI} !^index.php(\?.*)?$
RewriteRule ^(.*)$ index.php?p=$1 [L]

How to change %20 to - using htaccess with this code and how to get that value in php

Hello i am trying to change %20 to - using htaccess file but i am not being able to do it. Please have a look below code.
# Turn Rewrite Engine On
RewriteEngine on
# Rewrite for index.php
RewriteRule ^home index.php [L]
#Rewrite for inner.php?u=####
RewriteRule ^user/([0-9a-zA-Z]+) inner.php?u=$1 [L]
#Rewrite for article.php?id=1&title=Title Goes Here (unlimited number of space can be here)
RewriteRule ^article/([0-9]+)/([0-9a-zA-Z_-]+) article.php?id=$1&title=$2 [L]
and i am trying to get the value in article.php and echo the value using php like below
<?php echo $_GET['title'] ?>
The url looks like below. Their is %20 in the url but i want to convert it in -
http://localhost/learning/article/1/i%20am%20title
And when i try to echo the title value it only display i other are not shown. Please help me to fix it. Thank you in advance. :)
Please try with below .htaccess code. ([^/]+) REGEX code will allow all the chars in the querystring. So it will allow spaces and your querystring value will not broke down up to first space now.
# Turn Rewrite Engine On
RewriteEngine on
# Rewrite for index.php
RewriteRule ^home index.php [L]
#Rewrite for inner.php?u=####
RewriteRule ^user/([^/]+) inner.php?u=$1 [L]
#Rewrite for article.php?id=1&title=Title Goes Here (unlimited number of space can be here)
RewriteRule ^article/([0-9]+)/([^/]+) article.php?id=$1&title=$2 [L]
Apache automatically rewrites %20 into space (\s) but your rule doesn't accept it. This should work:
RewriteRule ^article/(\d+)/([0-9a-zA-Z_-\s]+) article.php?id=$1&title=$2 [L]
In your PHP code you will may need use urldecode(). However, I highly recommend to ignore title in GET parameter and to use only ID (don't forget to check if it is the right type (eg. integer)). Reading the title from your database is more secure.

Could i have 2 links from the same page with htaccess?

So my htaccess lines look like this:
RewriteRule ^meniu/([a-zA-Z0-9]+)/$ produse.php?categorie=$1
RewriteRule ^meniu/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/$ produse.php?categorie=$1&produs=$2
www.mysite.com/meniu/pizza/ works
www.mysite.com/meniu/pizza/Quatro_Formaggi/ doesn't work, it displays 404 not found.
Your URL has the underscore character
www.mysite.com/meniu/pizza/Quatro_Formaggi/
so just add the _ to the RewriteRule to match it
RewriteRule ^meniu/([a-zA-Z0-9]+)/$ produse.php?categorie=$1
RewriteRule ^meniu/([a-zA-Z0-9]+)/([a-zA-Z0-9_]+)/$ produse.php?categorie=$1&produs=$2
Your URL has a - (underscore character); and your rules don't; so you need to add the _ to the rule.
Also instead of using [a-zA-Z0-9] I would suggest using [a-z0-9] and the Not Case Sensitive flag ([NC]). So My suggested rules would be:
RewriteRule ^meniu/([a-z0-9]+)/$ produse.php?categorie=$1 [NC]
RewriteRule ^meniu/([a-z0-9]+)/([a-z0-9_]+)/$ produse.php?categorie=$1&produs=$2 [NC]
Also make sure you have a rule to add trailing slashes above this one or it will be annoying to any users hand entering the address to remember to have the trailing slash.

.htaccess 301 redirection removing white space [duplicate]

So here's my problem. I took over a site that has has a bunch of pages indexed that have %20 indexed in Google. This is simply because the person decided to just use the tag name as the title and url slug. So, the urls were something like this:
http://www.test.com/tag/bob%20hope
http://www.test.com/tag/bob%20hope%20is%20funny
I have added a new field for the url slug and string replaced all spaces with dashes. While I have no problem linking to these new pages and getting the data, I need to 301 redirect the old URLs to the new URLs, which would be something like:
http://www.test.com/tag/bob-hope
http://www.test.com/tag/bob-hope-is-funny
So, it needs to be able to account for multiple spaces. Any questions? :)
Use these rules in your .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
# keep replacing space to hyphen until there is no space use internal rewrite
RewriteRule ^([^\s%20]*)[\s%20]+(.*)$ $1-$2 [E=NOSPACE:1]
# when there is no space make an external redirection
RewriteCond %{ENV:NOSPACE} =1
RewriteRule ^([^\s%20]+)$ $1 [R=301,L]
This will replace all space characters (\s or %20) to hyphen -
So a URI of /tag/bob%20hope%20is%20funny will become /tag/bob-hope-is-funny with 301
Brief Explanation: If there are more than 1 space in URI then 1st RewriteRule is fired recursively replacing each space character with hyphen - until there is no space left. This rule will only rewrite internally.
Once no space is left 2nd RewriteRule is fired which just uses a 301 redirect to the converted URI.
Building on #anhubhava's answer, it's close, but that will also match %,2 or 0 in the URL, and it can cause a loop on apache 2.2 if you don't use the DPI parameter. The full script should look like this:
Options FollowSymlinks MultiViews
RewriteEngine on
RewriteBase /
# keep replacing space to hyphen until there is no space use internal rewrite
RewriteRule ^([^\s%20]*)(?:\s|%20)+(.*)$ $1-$2 [N,E=NOSPACE:1,DPI]
# when there is no space make an external redirection
RewriteCond %{ENV:NOSPACE} =1
RewriteRule ^([^\s%20]+)$ $1 [R=301,L]
I've also added the N (Next) parameter as this then forces the rules to be re-evaluated from the start straight after this rule if it matches. If this isn't there, you can get problems if you're using apache as a reverse proxy as it's unlikely that it'll get to the end of the rewrites before something else happens.

PHP all GET parameters with mod_rewrite

I am designing my application. And I should make the next things. All GET parameters (?var=value) with help of mod_rewrite should be transform to the /var/value. How can I do this? I have only 1 .php file (index.php), because I am usign the FrontController pattern. Can you help me with this mod_rewrite rules?Sorry for my english. Thank you in advance.
I do something like this on sites that use 'seo-friendly' URLs.
In .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /index.php [L]
Then on index.php:
if ($_SERVER['REQUEST_URI']=="/home") {
include ("home.php");
}
The .htaccess rule tells it to load index.php if the file or directory asked for was not found. Then you just parse the request URI to decide what index.php should do.
The following code in your .htaccess will rewrite your URL from eg. /api?other=parameters&added=true to /?api=true&other=parameters&added=true
RewriteRule ^api/ /index.php?api=true&%{QUERY_STRING} [L]
.htaccess
RewriteEngine On
# generic: ?var=value
# you can retrieve /something by looking at $_GET['something']
RewriteRule ^(.+)$ /?var=$1
# but depending on your current links, you might
# need to map everything out. Examples:
# /users/1
# to: ?p=users&userId=1
RewriteRule ^users/([0-9]+)$ /?p=users&userId=$1
# /articles/123/asc
# to: ?p=articles&show=123&sort=asc
RewriteRule ^articles/([0-9]+)/(asc|desc)$ /?p=articles&show=$1&sort=$2
# you can add /? at the end to make a trailing slash work as well:
# /something or /something/
# to: ?var=something
RewriteRule ^(.+)/?$ /?var=$1
The first part is the URL that is received. The second part the rewritten URL which you can read out using $_GET. Everything between ( and ) is seen as a variable. The first will be $1, the second $2. That way you can determine exactly where the variables should go in the rewritten URL, and thereby know how to retrieve them.
You can keep it very general and allow "everything" by using (.+). This simply means: one or more (the +) of any character (the .). Or be more specific and e.g. only allow digits: [0-9]+ (one or more characters in the range 0 through 9). You can find a lot more information on regular expressions on http://www.regular-expressions.info/. And this is a good site to test them: http://gskinner.com/RegExr/.
AFAIK mod_rewrite doesn't deal with parameters after the question mark — regexp end-of-line for rewrite rules matches the end of path before the '?'. So, you're pretty much limited to passing the parameters through, or dropping them altogether upon rewriting.

Categories