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.
Related
I want to set a rule in .htaccess if I enter in the url www.mydomain.com/compare.php set 'public_html' as root otherwise anything come in the url set root as 'public' folder.
RewriteRule ^(?!compare-source.php).*)$ public/$1 [L]
I want to achieve following result.
if url is www.mydomain.com/compare.php hit following file.
public_html/compare.php
if urls are www.mydomain.com/ OR www.mydomain.com/home etc hit following file.
public_html/public/index.php
I am weak in regex and in these apache rules always :-( can someone give me the solution with good description?
Your answers are welcome, please can you describe how this crazy things work in detail. Thanks.
Try:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public
RewriteRule ^((?!compare\.php).*)$ /public/$1 [L]
The RewriteEngine directive enables or disables the runtime rewriting engine.
The RewriteCond directive defines a rule condition. The following Rule is only used if this condition is met; In our case, if REQUEST_URI (the path component of the requested URL) does not (because of !) begin (because of ^) with /public. We need this condition because we don't want to rewrite already rewritten URL - that would cause loop and Internal error 500.
Finally, the RewriteRule will match regex Pattern (^((?!compare\.php).*)$) against part of the URL after the hostname and port, and without the leading slash. If the pattern is matched, the Substitution (public/$1) will replace the original URL-path.
In plain language, if URL path does not begin with compare.php (because of ?!), pick everything (.*) between beginning (^) and end ($) and place it in variable $1. Then replace the original URL path with /public/$1.
#Anubhava's answer is also correct, he just placed both conditions in RewriteRule, and also it could be written even more readable as:
RewriteCond %{REQUEST_URI} !^/public
RewriteCond %{REQUEST_URI} !^/compare\.php
RewriteRule ^(.*)$ /public/$1 [L]
You can use this .htaccess in site root:
RewriteEngine On
# route /home/ or /home to /
RewriteRule ^home/?$ / [L,NC]
# if not compare-source.php or public/* then route to /public/*
RewriteRule ^(?!public/|compare-source\.php$).*)$ public/$1 [L,NC]
I need to rewrite only 1 specific URL, to display to visitors specific content: I tried something like, this:
RewriteEngine on
RewriteCond %{REQUEST_URI} example.com/test/2_5/page.html
RewriteRule ^(.*)$ example.com/tt.html [R,L]
I need to rewrite all requests to:
http://example.com/test/2_5/page.html
to
http://example.com/tt.html
how to do this?
thanks,
Redirect /test/2_5/page.html /tt.html
Ok, mod_rewrite
RewriteRule ^/test/2_5/page.html /tt.html [L]
Remove first / if using .htaccess in the site's root folder, not the .conf file. And, typically, the final url should be the full one, with http:// and domain, but this one will work too. If you want to do everything by the rules then
RewriteRule ^/test/2_5/page\.html$ http://example.com/tt.html [L]
I was using .htaccess code to remove .php extension for all my web pages. Here's the code I use:
RewriteEngine On
RewriteCond /%{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9_-\s]+)/$ /$1.php
It doesn't seem to work. I think I'm missing something. When I type www.mysite.com/about/ to get www.mysite.com/about.php it returns error 404 (page not found). Can someone please shed some light.
Thanks,
Paul G.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# If folder does not exist
RewriteCond %{REQUEST_FILENAME} !-d
# and file exist
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
# uncomment the below rule if you want the "/" to be required
# otherwise leave as is
# RewriteRule ^([^/]+)/$ $1.php [L]
# internally show the content of filename.php
RewriteRule ^([^/]+)/?$ $1.php [L]
The above rule will:
will not redirect if a folder exist
will not redirect if the file does not exist
will redirect what comes before the / if one is present as the file name
So it will work for all these examples:
http://domain.com/about/
http://domain.com/about
http://domain.com/contact/
http://domain.com/contact
If you want you can remove the ?, like the commented rule, to make it accept only URL's that end with a /.
http://domain.com/about/
http://domain.com/contact/
Now these are important step for the above to work:
It must go into the .htaccess on your root folder for example /home/youraccount/public_html/.htaccess
The Options before the rewrite rule are very important specially -MultiViews
The file must exist on the same place the .htaccess is for example in your case the about.php file
The PHP must be working obviously.
It seems like the slash at the end of your rule might be there, or it might not. Adding a ? makes it optional, so that mysite.com/about and mysite.com/about/ will both match.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-\s]+)/?$ /$1.php
It's hard to say if this is what's causing your problem, or if something else is, though. Does mysite.com/about.php also give you an error?
I have the following code in my .htaccess.
RewriteEngine On
RewriteRule ^/(\w+)/?$ /?user=$1
I'm trying to rewrite
http://domain.com/?user=username into http://domain.com/username. Unfortunately this code doesn't rewrite anything. Please help
Note:
I checked phpinfo() and mod_rewrite is loaded.
Update
I need to get username from url like http://facebook.com/username. But this code rewrites every folder in root folder, so my /css folder become http://domain.com/css/?u=common. How to allow this code works only for http://domain.com/index.php
The mistake you are doing is the use of / in the beginning of the line ^/(\w+)/?$
rewrite rules strips off the / from the beginning of the pattern to be matched in .htaccess and directory context.
Try doing this:
RewriteEngine On
RewriteRule ^(\w+)/?$ /?user=$1
From RewriteRule Directive docs :
What is matched?
In VirtualHost context, The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string (e.g. "/app1/index.html").
In Directory and htaccess context, the Pattern will initially be matched against the filesystem path, after removing the prefix that lead the server to the current RewriteRule (e.g. "app1/index.html" or "index.html" depending on where the directives are defined).
If you wish to match against the hostname, port, or query string, use a RewriteCond with the %{HTTP_HOST}, %{SERVER_PORT}, or %{QUERY_STRING} variables respectively.
Edit: Answer updated as per OP's request:
Add this :
RewriteEngine On
#do nothig if URL is trying to access the folder CSS.
RewriteRule *css/* - [L]
#checks where the URL is a valid file/folder.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w+)/?$ /?user=$1
I think that you are doing it the right way round, but explained it the wrong way round!
Is the problem that you don't need the initial / as the URL passed to test doesn't include it!?
I suspect it should be RewriteRule ^(\w+)/?$ /?u=$1
Also, be careful you don't end up with a loop!
I need help with my mod_rewrite for a site im currently working on.
Let's say I have this site http://example.com
And I want to be able to make any value after the / to route to page.php like below
http://example.com/value1
http://example.com/value2
to point to
http://example.com/page.php?id=value1
http://example.com/page.php?id=value2
,respectively.
But, not route to that page when im pointing to "admin"
http://example.com/admin/
I've tried
RewriteEngine on
RewriteCond $1 !^(admin)
RewriteRule ^(.*)$ /page.php?id=$1 [L]
But it isn't working. Any thoughts?
$1 is not available at the time of the Condition. I believe what you are looking for is close to:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^admin/.*
RewriteRule ^(.*)$ page.php?id=$1 [L]
this rule causes everything not starting with admin/ to go to /page.php. I don't believe the %{param} is optional. Using RewriteBase / means you do not to have prepend / on /admin and /page.php; it may actually fault if you use /page.php instead of page.php
If you have means of accessing the server values, then the final rule can be:
RewriteRule . page.php [L]
You can find the called url in the REQUEST_URI
This of any help?
.htaccess mod_rewrite - how to exclude directory from rewrite rule