I have a problem with a rewriterule in my .htaccess, I would like to redirect like this:
www.mywebsite.com/category?p=2
to
www.mywebsite.com/category
where category is variable.
I have my rewrite engine on and other redirects are working fine. This is what what I got so far:
RewriteRule ^/([a-z_\/]*)\?? /$1 [R=301,L]
What is the best way to accomplish this?
Your request looks strange, but anyway, this should do it:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^p=
RewriteRule /?([a-zA-Z0-9]+)/? /$1? [R=301,L]
Use this:
RewriteEngine On
RewriteRule ^category$ /category?p=2 [L]
It will give you the following URL:
www.mywebsite.com/category
Related
I want to redirect from a page to another with parameters with htaccess.
For example:
From: www.websitename.co.uk/mylink.php?u=*username*
To: www.websitename.co.uk/mylink/*username*
I tried this but it's not working.
RewriteEngine on
RewriteRule www.websitename.co.uk/mylink.php?u=*username* www.websitename.co.uk/mylink/*username* [R=301]
Where am I doing wrong?
Working fine for me , hope this will work fine.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/mylink\.php$
RewriteCond %{QUERY_STRING} u=([a-zA-Z]+)
RewriteRule .* http://websitename.co.uk/mylink/%1? [R=301]
Rewrite rules use regex like this:
RewriteEngine on
RewriteRule ^/mylink.php?u=(.*) /mylink/$1 [R=301]
Reference: https://httpd.apache.org/docs/current/rewrite/intro.html
I have this following url localhost/test/index.php?page=1 And i want it to be
'index.php/page/1'
so far I have this :
RewriteEngine On
RewriteRule ^page/([0-9]+)/?$ index.php?page=$1 [NC,L]
But it's not working .Any suggestion what am i doing wrong ?
Try with this
RewriteRule ^page/([^/]*)$ index.php/?page=$1 [L]
If you set it correct then URL like this http://localhost/test/page/1 should work
Try this one
RewriteEngine on
RewriteRule ^(.*)/page/([0-9]+)$ /index.php?page=$2 [NC,L]
The url will be
localhost/test/index.php/page/123
If you want the url looks like
localhost/test/index.php/page/123/
change you rule into this one
RewriteEngine on
RewriteRule ^(.*)/page/([0-9]+)/$ /index.php?page=$2 [NC,L]
RewriteRule ^index.php\/page\/(.*?)\/?$ index.php?page=$1 [NC]
The above code should solve your probleem. But you may want to read up on mod_url_rewrite
See https://www.sitepoint.com/guide-url-rewriting for more info
I have a slight problem.
I have a site that uses these formats for viewing certain PHP documents:
domainname.com/server.php?id=14
domainname.com/changeserver.php?id=14
domainname.com/customize.php?id=14
I would like to make these:
domainname.com/server/14
domainname.com/changeserver/14
domainname.com/customize/14
I also have various URLs such as /index.php and /login.php I would like to clean up.
If someone were to type the original URL, such as /server.php?id=14, I would like it to redirect to it's clean URL.
If anyone can provide me with a config or help me along the way to making one that can do this, it would be greatly appreciated.
To prevent an infinite loop while externally redirecting the ugly url to the clean url and internally rewriting it back to the ugly url, you can use the THE_REQUEST trick.
#External redirect
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /(server|changeserver|customize)\.php\?id=([^&]+)\ HTTP
RewriteRule ^ /%2/%3? [R,L]
#Change above to [R=301,L] once all rules work as expected
#Internal rewrite
RewriteRule ^(server|changeserver|customize)/(.*)$ /$1?id=$2 [L]
You need to rewrite your URLs in .htaccess file.
RewriteEngine On
RewriteRule ^server/(.*)$ server.php?id=$1 [R=301,L]
here is a good guide on URL rewriting
http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
In this case you have to use a 301 Redirect of URLS. Here is an example:
Redirect
www.example.com/a1/articles.php?id=1&name=abc
to
www.example.com/article/1/abc
For this you have to modify the .htaccess file with the following:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=(.*)&name=(.*)$
RewriteRule ^articles\.php$ /article/%1/%2? [R=301]
RewriteRule ^article/([^-]+)/([^-]+)$ /articles.php?article_id=$1&article_name=$2 [L]
RewriteEngine On
RewriteRule ^server/([A-Za-z0-9-_,()'+]+)?$ server.php?id=$1
RewriteRule ^changeserver/([A-Za-z0-9-_,()'+]+)?$ changeserver.php?id=$1
RewriteRule ^customize/([A-Za-z0-9-_,()'+]+)?$ customize.php?id=$1
To adapt to your code. Here is the answer.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^server\.php$ /server/%1? [R=301]
RewriteRule ^server/([^-]+)$ /server.php?id=$1 [L]
Check the Above code. I think this will help you.
I went through a bunch of websites and tutorials yet can't find a solution.
Following snippet works and http://example.com/page/pot return a pot.php content
RewriteEngine on
RewriteRule ^page/([^/]*)$ $1.php?page=$1 [L]
I can't get it to work the other way around
RewriteEngine on
RewriteRule ^([^/]*)/page$ $1.php?page=$1 [L]
Your current approach will cause infinite looping since Apache re-injects rewritten URI back for further rule processing.
You need to use THE_REQUEST variable for that like this:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+page/[^.]+\.php\?page=([^\s&]+) [NC]
RewriteRule ^ page/%1? [R=302,L]
# internal forward from pretty URL to actual one
RewriteRule ^page/([^/]*)/?$ $1.php?page=$1 [L,QSA,NC]
Try to use:
RewriteEngine on
RewriteRule ^([^/]+)/page$ $1.php?page=$1 [L]
I'm using php switch[_get] in my menu system to create url.com/?p=page and I'd like that to change into url.com/page.html. But I can't make it work, maybe some of you know the right settings for this.
I'm currently using this as .htaccess:
RewriteEngine On
RewriteBase /
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^start(.*)\.html$ ?p=start$
Thank you!
Update:
I tried with the $1 but still the url is: ?p=start when I want it to be /start.html
Looks like you're missing the $1 in your last rule:
RewriteRule ^start(.*)\.html$ ?p=start$1
^^^^
EDIT After new information, try:
RewriteCond %{QUERY_STRING} p=([a-z0-9]+) [NC]
RewriteRule . /%1.html [L]
This captures the p= parameter from the querystring and uses it to rewrite to page.html
Try
RewriteRule ^start(.*)\.html$ ?p=start$1 [L]
See the one (1) at the end.
htaccess works the other way around.
It makes things like /start.html proxy to ?p=start however, you still have make the links themselves target /start.html.
So change all the <a href="?p=start"> to <a href="/start.html">.
you need
RewriteCond %{QUERY_STRING} ^p=(.+)$ [NC]
RewriteRule ^$ /%1.html? [R=301,L]
R=301 is to change the url in the browser, but
you still need to update all links on the site:
<a href="?p=start"> to <a href="/start.html">
Edit: Try the updated one (it has a ? after html)