Rewrite Url (URL Routing) in .htaccess - php

My directories are like:
http://www.mydomain.com/macbook-computer
http://www.mydomain.com/sony-computer
http://www.mydomain.com/lenovo-computer
I want to make that, if a user type computers/macbook-computer like:
http://www.mydomain.com/computers/macbook-computer
I want to display page which is : http://www.mydomain.com/macbook-computer.
My restrictions are:
1. User must type /computers (segment 1)
2. String coming from computers/ must end with "computer". (segment 2)
How can I make this achieve in my .htaccess file?

You may try this:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/computers/([^-]+)-computer/? [NC]
RewriteRule .* %1-computer? [R=301,L]
Redirects permanently
http://www.mydomain.com/computers/anyname-computer with or without trailing slash.
To:
http://www.mydomain.com/anyname-computer
Strings computers and computer are assumed to be fixed, while anything is assumed to be variable.
The incoming URL structure has to be kept for the rule-set to work: First folder /computers followed by /anyname-computer.
For silent mapping, remove R=301 from [R=301,L]

You need a rewrite rule in your .htaccess file, and a little regular expression magic. Something like this should do the trick
RewriteRule ^computers/macbook-computer$ http://www.mydomain.com/macbook-computer
Here's a nice online tool for checking rewrite rules
http://htaccess.madewithlove.be/

Related

Rewrite automatically removes backslash if there's more than one?

I have a very simple url rewriting rules:
RewriteEngine on
RewriteCond %{HTTP_HOST} !script.php
RewriteRule ^test/(.*) script.php?q=$1
The idea is to have this kind of urls: http://mywebsite.com/test/http://example.com
and then send http://example.com to the script.php as a query parameter. The problem is that I'm receiving http:/example.com instead of http://example.com. Also, http:////example.com would be sent as http:/example.com. What causes this behavior ?
Apache mod_rewrite engine converts multiple ///... into single / for pattern matching in RewriteRule directive. However if you match it using RewriteCond then you can match multiple /s.
You can use rule like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/+test/+(https?://.+)$ [NC]
RewriteRule ^ script.php?q=%1 [L,QSA]
The browser causes this behaviour. It contracts a sequence of / into 1 /, because it is still essentially a path. ///// does not change the directory we are in, so we could as well use /.
You have two options:
Change your links to use a query string instead. If you rewrite test/?q=something to script.php?q=something everything works as expected. You would do the following:
RewriteRule ^test/?$ script.php [L]
Since you don't alter the query string, the original query string is automatically copied to the new query string.
Don't make an assumption on how many slashes you will encounter. The url might not look correctly in the url bar of the browser, but if it is just a redirect, it will only be visible for a very short period of time.
RewriteRule ^test/(http|https):/+(.*)$ script.php?q=$1://$2

htaccess redirect from asp to php with parameters

I have to redirect all my asp page to new website developed in PHP.
I had my asp page as,
abc.asp?id=82
Which need to be redirected to
siteurl/index.php/abc
Can any one help me with this in HTAccess?
I have tried with,
Redirect 301 /abc.asp?id=82 siteurl/index.php/abc
rewriterule ^/abc.asp?id=82$ siteurl/index.php/abc[R=301,L]
But this is not working and giving me a 404 error.
You can't match against the query string in either a Redirect or RewriteRule. It's not very clear what the scope you're trying to accomplish here. Is the id=82 important? Does abc mean "anything that's just letters"? Is siteurl a directory or a domain name? If it's strictly what you've attempted, then this is strictly how it'll work:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=82($|&)
RewriteRule ^/?abc.asp$ siteurl/index.php/abc? [L,R=301]
You're making 2 main mistakes:
Rewrite rule matches only URI part and doesn't match query string
Rewrite rule in .htaccess doesn't match leading slash.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^id=82(&|$) [NC]
RewriteRule ^(abc)\.asp$ http://domain.com/index.php/$1 [R=302,L,NC]
Once you verify it to be working replace 302 (Temporary Redirect) with 301 (Permanent Redirect)
Just use
RewriteRule ^abc.asp$ siteurl/index.php/abc [R=301,QSA,L]
That should do the job.. Using the QSA flag will forward any GET paramater too, if that's what you meant with the title.

Apache mod_rewrite module issue

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!

URL Rewrite rule in .htaccess for Root directory

I am using this rule to rewrite the link
RewriteEngine On
RewriteRule ^(.*)/(.*) show_cv.php?email=$1
It is working fine like if I write this url with last slash
www.mysite.com/letschat_2008#yahoo.com/ ----> index.php?email=letschat_2008#yahoo.com
But when I remove the last slash from the link www.mysite.com/letschat_2008#yahoo.com/ it shows error 404.
I wish the URL Rewrite rule would work for both with slash and without slash (/)
www.mysite.com/letschat_2008#yahoo.com/ ----> index.php?email=letschat_2008#yahoo.com
www.mysite.com/letschat_2008#yahoo.com ----> index.php?email=letschat_2008#yahoo.com
Your rules are looping, you need to make sure you are rewriting an email address, and add some conditions so that the rule doesn't get applied if it's accessing an existing resource:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9_\-\#\.]+)/?$ /show_cv.php?email=$1 [L]
You should then be using the following rule:
RewriteRule ^(.*)/? show_cv.php?email=$1
I assume you note these rules in a .htaccess file, not in the server configuration when looking at your description ?
Rethink if you don-t want to put this into the server configuration. Apart from the usage of .htaccess files being harder to debug using rewrite rules in those files is more complex than in the server configuration. This is documented in mod_rewrites docs.
The reason for the behaviour is the different content of REQUEST_URI in both cases. Have a try checking this directly and you will see the problem. The whole part "letschat_2008#yahoo.com" is simply missing in that variable in case 2 (no "/"). To get this working you must use an additional rewriteCondition (also documented...). Something like these untested lines:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(.+)&
RewriteRule - show_cv.php?email=%1
(note the '%' instead of a '$' in the last line)

Little mod_rewrite problem

I have a classifieds website. Each classified is linked like this originally:
mydomain.com/ad.php?ad_id=Bmw_M3_M_tech_113620829
What RewriteRule should I use to make this link look like:
mydomain.com/Bmw_M3_M_tech_113620829
Also, what do I need to add to my .htaccess file?
This is what I have so far:
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
And I have just enabled mod_rewrite which was disabled at first on my Ubuntu server by using:
a2enmod rewrite
Anything else I need to know or do?
Thanks
It looks like you need something like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/ad\.php
RewriteRule ^(.*)$ ad.php?ad_id=$1 [L]
This should rewrite a request to mydomain.com/Bmw_M3 into mydomain.com/ad.php?ad_id=Bmw_M3.
The RewriteCond excludes direct requests to ad.php from being rewritten. The RewriteRule would simply substitutes anything after mydomain.com/ in place of the $1. The [L] (last) flag stops the rewriting process so that it won't apply any more rewrites for a request that is rewritten by this rule.
You need to add the rule itself. Requires regex =)
RewriteRule ([a-zA-Z0-9_]+) ad.php?ad_id=$1
Of course, this regex should be fine tuned based on the ad ID you're passing on - by telling what characters can be in that ad ID and similar. For example, if all ad IDs are ending with and underscore and 9 digits, something like this:
RewriteRule ([a-zA-Z0-9_]_[0-9]{9}) ad.php?ad_id=$1
Oh, also, after enabling mod_rewrite restart apache. Make sure that AllowOverride is not set to None, cause in that case, apache will ignore .htaccess files in directories.

Categories