I'm interested in rewriting a url which is already "friendly" in order to clean up what is already in the path.
At the moment, I have a URI which points to http://example.com/page/index/home, but ideally id like to have the remove page/index completely, and just have http://example.com/home.
Something like the following...
RewriteEngine On
RewriteRule ^page/index/home$ /home [L]
If you know the specific page you are matching
To achieve what you want, you can just do this:
RewriteEngine On
RewriteRule ^/home$ /index.php [QSA,L]
QSA stands for Query String Append. It will append the query string (your GET parameters) to the end of your URL automatically if there is one.
You can use this rule in .htaccess file:
RewriteEngine On
RewriteRule ^home$ page/index/home [NC]
Related
What I have is a url like this:
domain.com/index.php?chapter=chapter-name
and a url link this:
domain.com/index.php?marker=marker-name
What I want is:
domain.com/#chapter-name
and this:
domain.com/#chapter-name/marker-name
How can I do this with .htaccess?
I now have the following:
RewriteEngine On
RewriteRule ^(.*)/$ http://%{HTTP_HOST}/$1
RewriteRule ^([a-zA-Z0-9_-]+)?$ index.php?chapter=$1
RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php?chapter=$1
Thanks!
To get the chapter working, try this rule:
RewriteRule ^index.php\?chapter=([a-zA-Z0-9_-]+)$ http://www.%{HTTP_HOST}/#$1
Your second needs is a arbitrary... how does the marker "know" which chapter... Maybe you meant this on your second request:
domain.com/index.php?chapter=chapter-name&marker=marker-name
You can't. Basically # is a client thing, and never sent to the server. .htaccess therefore will not work.
I have several URLs which are dealt with by rewrite rules. Basically I want to add some $_GET-vars to a rewritten URL.
Say I have:
http://www.domain.com/search/the-netherlands/overijssel/
I would like to add some extra data to the URL like:
http://www.domain.com/search/the-netherlands/overijssel/?custom_var=1
My RewriteRule looks like this:
RewriteEngine On
RewriteRule ^search/(.*)/(.*)/$ /index.php?page=Search&Country=$1&Region=$2 [L]
When I var_dump the contents of $_GET I don't see custom_var anywhere. Can this be done, using the existing rewrite rule?
Since your query already has some parameters, append [QSA] to the end of the RewriteRule.
RewriteRule ^search/(.*)/(.*)/$ /index.php?page=Search&Country=$1&Region=$2 [QSA,L]
I have a single get data (affid) that i want to retrieve using .htaccess but i don't know how to do it. Can you help me out?
Here is an example of links:
mysite.com/searchmembers?affid=1001
mysite.com/profle/123?affid=1002
mysite.com/videos/567?affid=1003
Another thing that might give a problem is these links already have been rewritten on .htaccess
RewriteRule ^searchmembers? index.php?task=searchMembers
RewriteRule ^profile/(.*)? index.php?task=viewProfile&id=$1
RewriteRule ^videos? index.php?task=videos&id=$1
i just want to retrieve the affid and add it to the links like this:
RewriteRule ^searchmembers... index.php?task=searchMembers&affid=(data retrieved)
RewriteRule ^profile/(.*)... index.php?task=viewProfile&id=$1&affid=(data retrieved)
RewriteRule ^videos? index.php... task=videos&id=$1&affid=(data retrieved)
i know i could add it on htaccess for each of these links but if there is an easier way to do this then it would be a great help. thank you for any response that i will receive!
Add this to your .htaccess in your web root / directory
RewriteEngine on
RewriteBase /
RewriteRule ^searchmembers$ index.php?task=searchMembers [QSA,NC,L]
RewriteRule ^profile/(.*)$ index.php?task=viewProfile&id=$1 [QSA,NC,L]
RewriteRule ^videos/(.*)$ index.php?task=videos&id=$1 [QSA,NC,L]
If you just need to append a new query parameter (like task here), any existing query parameters can be appended automatically using the [QSA] (Query String Append) flag. I've also corrected the regex used in your RewriteRules. The use of ? is incorrect.
I have a URL like index.php?url_id=u5531e3aas01fe5c2 and I also want to make it work like /u5531e3aas01fe5c2, that it to pass a parameter to my PHP, but something is wrong with my code.
RewriteEngine On
RewriteBase /try/
RewriteRule /u[a-z0-9]{17} /index.php?url_id=$1 [L,QSA]
P.S.
And also is it possible to rewrite URL that if appears index.php?url_id=u5531e3aas01fe5c2, it would be rewritten to /u5531e3aas01fe5c2
Try this:
RewriteRule ^u([a-z0-9]{16}) /index.php?url_id=$1 [L,QSA]
I currently have a working URL:
http://example.com/security-services.php?service=fixed-camera-surveillance
and then I have PHP say, $_REQUEST['service'] to do some stuff...
But I'd like to achieve the same function if the URL looked like this:
http://example.com/security-services/fixed-camera-surveillance
Thanks!
An .htaccess file with something like this should do it.
Options +FollowSymLinks
RewriteEngine On
RewriteRule security-services/(.*)/? security-services.php?service=$1 [L]
The part that says security-services/(.*)/? matches the URL in the browser and rewrites it to security-services.php.
The key part is the (.*) which captures that portion of the URL and passes it to the PHP script as a GET value.
Try this rule:
RewriteEngine on
RewriteRule ^security-services/([^/]+)$ security-services.php?service=$1 [L]
The [^/]+ describes the next path segment after /security-services, (…) forms a group that’s match then can referenced to with $1 in the substitution.
And if you want a more general for any kind of …-service.php file:
RewriteEngine on
RewriteRule ^([^/-]+)-services/([^/]+)$ $1-services.php?service=$2 [L]
You can create a rule like this:
RewriteRule ^security-services/(.*)/? /security-services.php?service=$1
If you're using a .conf file, change ^ to ^/