I have problem and dont have any idea how to solve it.
I have URL like this:
http://somedomain.com/link.php?lnk=1&sid=dsds
and need to change it to this:
http://somedomain.com/link/1/dsds
If there is only one variable I use this:
RewriteEngine on
RewriteRule ^link/([a-zA-Z0-9_-]+)$ link.php?lnk=$1
But I don't have any idea how to attach that second variable &sid={something}
Thanks in advance for any help.
RewriteEngine on
RewriteRule ^link/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ link.php?lnk=$1&sid=$2
if the first parameter/value uses digits only, then you can use
RewriteEngine On
RewriteRule ^link/([0-9]+)/([a-zA-Z0-9_-]+)$ link.php?lnk=$1&sid=$2 [L]
Apache mod_rewrite Introduction (see "Figure 1" in "Regex Back-Reference Availability" section)
RewriteRule Flags
Try this:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^lnk=(.+)\&sid=(.+)
RewriteRule ^link\.php link/%1/%2/? [R=301,L]
Related
My original file is a PHP page named search.php.
Normally, the search function will be:
mydomain.com/search.php cid=&type=search&q=keyword+text&page=2
However, is it possible to rewrite it to mydomain.com/all.html?q=keyword+text&page=[1-anypage]?
I tried to put this code into .htaccess:
RewriteRule ^all.html?q=([^.*]+)&p=([0-9]+)$ search.php?cid=&type=search&q=$1&p=$2 [L]
but something went wrong.
Please help me find the solution. Thanks.
Yes its possible but you can't match against query string in pattern of a RewriteRule, you will need to use RewriteCond directive to check %{QUERY_STRING} something like the following
RewriteEngine On
RewriteCond %{QUERY_STRING} ^q=([^&]+)&page=(.+)$ [NC]
RewriteRule ^all\.html$ /search.php?cid=&type=search&q=%1page=%2 [L]
the incoming URL
domain.com/12/3
should be re-written to
domain.com/?w=12&h=3
htaccess
RewriteEngine on
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_-]+)$ index.php?w=$1&h=$2 [QSA]
the php
<?php
echo $_GET['h'];
?>
Result
404 page
I've tried using htaccess to change the result of the url and then retrieve the value from the URL, could someone help me out?
Maybe you need to escape - like:
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_\-]+)$ index.php?w=$1&h=$2 [QSA]
PS. I hope the above URL /12/3 is just for example because your regex accepts only a-z
RewriteEngine On
RewriteRule ^[a-z]{2,2})/([a-zA-Z0-9_-]+)$ /index.php?w=$1&h=$2 [QSA]
Essentially, I'm pretty sure you need a / in front of index.php
I like to use this generator for my mod-rewrite rules.
http://www.generateit.net/mod-rewrite/
Try changing your rewriterule to the following:
RewriteEngine on
RewriteRule ^([a-z0-9]{2})/([a-zA-Z0-9_-]+)$ /index.php?w=$1&h=$2 [QSA]
Your example has two digits as the first parts of the path, which won't be matched by [a-z].
RewriteEngine On
RewriteRule ^(.*)/(.*)$ index.php?w=$1&h=$2 [QSA]
The above sorted it. Thanks for the replies
I want to rewrite my apache mod_rewrite pattern. I use to do with following:
RewriteEngine on
RewriteRule /^([^A-Z0-9a-z]+)\.html /search.php?search_id=$1&%{QUERY_STRING}
With the above rule I can't get what I expecting.
Can anyone tell what is wrong with my code and how to solve it.
Edited.
my requirement is simple.
I want to redirect if i enter /1.html to /search.php?search_id=1
Thats it.
This should work:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !search\.php
RewriteCond %{REQUEST_URI} ^/([0-9]+)\.html [NC]
RewriteRule .* search.php?search_id=%1 [L,QSA]
Maps silently
http://example.com/FileID.html
To:
http://example.com/search.php?search_id=FileID
search.php is considered a fixed string, while FileID can be any number.
For permanent redirection, replace [L,QSA] with [R=301,L,QSA]
You have a typo in your rule try this:
RewriteEngine on
RewriteRule ^/([^A-Z0-9a-z]+)\.html /search.php?search_id=$1&%{QUERY_STRING}
By the way you could also try to use this:
RewriteRule ^/?([^A-Z0-9a-z]+)\.html /search.php?search_id=$1&%{QUERY_STRING}
This makes the leading slash optional.
Are you typing 1.html after the domain? That is if your domain is example.com, then you type example.com/1.html?
If it is you are doing then don't use / before the regular expression in .htaccess file.
Try adding the following into your main .htaccess file. That is located in the directory which has your home page of the website:
RewriteEngine On
#RewriteBase /
RewriteRule ^([A-Za-z0-9]+)\.html/?$ /search.php?search_id=$1 [NC, L]
I have used RewriteBase as a comment. If you use a shared server then keep it as a comment or delete the line.
EDIT: [NC, L] use to tell that, NC flag for rule no need to be case sensitive (no case) and L flag for stop if this was matched.
I'm making a redirect script for my site, I use htaccess to rewrite the URL so it looks nicer.
eg. http://localhost/r/http://google.com is the URL, but when I printing the value it shows up like this http:/google.com.
One / is missing, how can I fix that?
Edit:
Rewrite rule:
RewriteRule ^r/(.*)/$ /system/offsite/redirect/index.php?url=$1 [L]
Thanks for any help :)
This behavior is due to Apache that removes empty path segments before mapping it. But you can access the original requested URI path via THE_REQUEST:
RewriteCond %{THE_REQUEST} ^GET\ /r/([^\ ]+)
RewriteRule ^r/ /system/offsite/redirect/index.php?url=%1 [L]
Use php urlencode function
EDIT:
//echo your url
echo 'http://localhost/r/'. urlencode('http://google.com');
and in your index.php file
//get your url
$url = urldecode($GET['url']);
I think REQUEST_URI variable will have correct text. Use it like this:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/r/(.*)$
RewriteRule ^r/ /system/offsite/redirect/index.php?url=%1 [L,QSA,NC]
I'm trying to use mod_rewrite to achieve the following rewrite:
From - http:// pre.domain.com/public/?project=Awesome
To - http:// pre.domain.com/project/Awesome
Can't seem to figure it out (despite reading through endless sites). Any help?
RewriteEngine On
RewriteRule ^project/(.*) /public/?$1 [L,NC]
RewriteEngine On
RewriteCond %{QUERY_STRING} ^project=(.*)$
RewriteRule ^/public/ /project/%1/? [R,L] # strip off query string
You need to match against the query string using a RewriteCond.
Not sure you have your URLs around the right way so I think your are trying to do this:
RewriteRule ^project/(Awesome) /public/?project=$1 [L]