original url - example.com/art.php?a=lorem
want to be visible as - example.com/art/lorem
litespeed server - htaccess is enabled
RewriteEngine ON
RewriteRule ^art/(.*)$ /art.php?a=$1 [L]
doesn't work
url is still - example.com/art.php?a=lorem
pls help
You just have a rewrite rule to rewrite example.com/art/lorem but you are missing a redirect rule to redirect example.com/art.php?a=lorem to example.com/art/lorem.
You may use:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+art\.php\?a=([^\s&]+) [NC]
RewriteRule ^ /art/%1? [R=301,L]
RewriteRule ^art/([\w-]+)/?$ art.php?a=$1 [L,QSA]
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of other rewrite directives. Example value of this variable is GET /index.php?id=123 HTTP/1.1
References:
Apache mod_rewrite Introduction
Apache mod_rewrite Technical Details
Apache mod_rewrite In-Depth Details
.htaccess tips and tricks
Related
I've been pulling my hair out trying to get a URL rewrite rule to work using .htaccess. Mod_rewrite is enabled and I have managed to get a 301 redirect to work (from /beta to /Beta/) so I know the .htaccess is able to work.
Basically I'm trying to get /Beta/Page.php?id=page&tab=services&tabid=tab1 to become /page/services (and ideally leave out the tabid if it's not going to break the site removing it).
The code I'm working with currently is:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^/Beta/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ /Beta/Page.php?id=$1&tab=$2&tabid=$3
redirect 301 /beta http://www.example.com/Beta/
Any help would be gratefully received.
Remove the leading slash:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(Beta)/[^.]+\.php\?id=([^&]+)&tab=([^\s&]*)&tabid=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/%3/%4? [R=302,L]
RewriteRule ^Beta/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ /Beta/Page.php?id=$1&tab=$2&tabid=$3 [L,QSA]
This will externally redirect:
/Beta/Page.php?id=page&tab=services&tabid=tab1
to
/Beta/page/services/tab1
and rewrite same URI internally.
Also .htaccess is per directory directive and Apache strips the current directory path (thus leading slash) from RewriteRule URI pattern.
im doing a cms at the moment
now im struggeling with the ajax implementation
i have everything running except a mod_rewrite problem..
RewriteEngine On
RewriteRule \.html index.php [L]
this redirects nothing except html files to index.php
i need a second rule witch checks the REQUEST_URI for a parameter to prevent the full site gets loaded by ajax.
i dont think this is understandable so i just post what i want to achieve^^
RewriteEngine On
RewriteCond %{REQUEST_URI} !(?rewrite=no$)
RewriteRule \.html index.php [L]
i want nothing redirected except html files and also no redirect on url's with "(.html)?rewrite=no" at the end
hope someone can help me since rewrites and regexp are not my stongest stuff
thanks in advance
From the Apache docs:
REQUEST_URI
The path component of the requested URI, such as "/index.html". This notably excludes the query string which is available as as its own variable named QUERY_STRING.
So you are actually looking to match on %{QUERY_STRING} rather than %{REQUEST_URI}. Don't include the ? on the query string when matching its condition:
RewriteEngine On
# Match the absence of rewrite=no in the query string
RewriteCond %{QUERY_STRING} !rewrite=no [NC]
# Then rewrite .html into index.php
RewriteRule \.html index.php [L]
I want to rewrite a simple url, but without generating google errors
This code works :
RewriteEngine on
RewriteRule lieu/([0-9]+).* index.php?com=location&lID=$1 [L]
RewriteRule evenement/([0-9]+).* index.php?eID=$1 [L]
but i want to add R=301 flag for SEO
When i add [R=301,L] :
The requested URL /var/www/mysite/index.php was not found on this server.
I know that R=301 flag must be used with http://
but when i try the url is not rewritting
Apache tries to guess whether a path is a URI-path or a file-path, and it's guessing wrong. When you are internally rewriting, a file-path is perfectly fine, because it's all internal to the server. But when you are redirecting, apache incorrectly guesses that your target (the index.php?eID= ) is a file-path and it gets flagged to be handled by mod_alias as a redirect. By the time the redirect happens, it's malformed as a file-path instead of URI path. That's why you're getting the /var/www/mysite/ bit when you redirect.
Either add a RewriteBase to provide a URI base for relative URIs, or make your target an absolute URI:
RewriteEngine On
RewriteBase /
RewriteRule lieu/([0-9]+).* index.php?com=location&lID=$1 [R=301,L]
RewriteRule evenement/([0-9]+).* index.php?eID=$1 [L,R=301]
or
RewriteEngine on
RewriteRule lieu/([0-9]+).* /index.php?com=location&lID=$1 [L,R=301]
RewriteRule evenement/([0-9]+).* /index.php?eID=$1 [L,R=301]
I have this url
http://www.mmametals.com.php5-20.dfw1-1.websitetestlink.com/?page_id=61
and i want to redirect to the root like this
http://www.mmametals.com.php5-20.dfw1-1.websitetestlink.com
here is my htaccess
RewriteEngine on
redirect 301 /?page_id=61/ http://www.mmametals.com.php5-20.dfw1-1.websitetestlink.com
but nothing is happening...any ideas
Also I was wondering if there is a way also in .htaccess to delete a duplicate /about ...so for example if the url is
http://somesite.com/about/about
it will rewrite the rule to always be
http://somesite.com/about
RewriteEngine On
RewriteCond %{QUERY_STRING} ^page_id=61$
RewriteRule (.*) http://www.mmametals.com.php5-20.dfw1-1.websitetestlink.com? [R=301,L]
RewriteCond %(REQUEST_URI) ^/about/about[/]?
RewriteRule (.*) http://somesite.com/about? [R=301,L]
should do it.
I don't know the exact answer off the top of my head as I don't work directly with apache that much any longer, but I think you need to look into apache's mod_rewrite module. Try taking a look at:
Apache's mod_rewrite documentation
This post about blocking certain URLS
I want to redirect a link to another with .htaccess file in Linux host. Can you help me?
from: http://example.com/examp
to: http://example.com/examp.php
And another one for my other site
from: http://example.com/examp
to: http://example.com/user.php?u=examp
You will need mod_rewrite enabled for this. Start with placing these lines into .htaccess:
RewriteEngine On
RewriteBase /
TBH I'm not 100% sure what do you mean exactly by permalink and how do you want to redirect, so I will provide 2 variants for each URL: rewrite (internal redirect) and redirect (301 Permanent Redirect).
1. This will rewrite (internal redirect) request for http://example.com/examp to http://example.com/examp.php while URL will remain unchanged in browser:
RewriteRule ^examp$ examp.php [L]
2. This will do the same as above but with proper redirect (301 Permanent Redirect) when URL will change in browser:
RewriteRule ^examp$ http://example.com/examp.php [R=301,L]
3. This will rewrite (internal redirect) request for http://example.com/examp to http://example.com/user.php?u=examp while URL will remain unchanged in browser:
RewriteRule ^examp$ user.php?u=examp [QSA,L]
4. This will do the same as above but with proper redirect (301 Permanent Redirect) when URL will change in browser:
RewriteRule ^examp$ http://example.com/user.php?u=examp [QSA,R=301,L]
Useful link: http://httpd.apache.org/docs/current/rewrite/
You will need mod_rewrite enabled for this
from: http://example.com/123
to: http://example.com/index.php?q=123
RewriteEngine on
RewriteBase /
RewriteRule ^/?([-A-Za-z0-9]+)/?$ index.php?q=$1 [QSA,L]
You'll want to look at RewriteRules and know/understand regular expressions. It'll be something like this:
RewriteEngine on
RewriteRule ^(.*)\/examp$ /examp.php [R=301,L]
- and -
RewriteRule ^(.*)\/[a-zA-Z0-9\-\_\.]+$ /user.php?u=$1 [R=301,L]
The latter example will take what's in-between the [] and place it in the $1 variable
Here is a good link to get you started:
http://www.webweaver.nu/html-tips/web-redirection.shtml