Htaccess redirecting in the wrong way - php

I'm using a simple HTAccess redirect rule to redirect :-
http://localhost/sell_script/s.php?value=1
to
http://localhost/sell_script/s/1
When I directly load http://localhost/sell_script/s.php?value=1, it works fine. But when I load http://localhost/sell_script/s/1 it again redirects me to http://localhost/sell_script/s.php?value=1.
What might be the problem here?
My htaccess :-
RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteRule ^s/(.*) sell_script/s.php?value=$1 [R]

I've written this simple re-write for you.
#ErrorDocument 404 /
RewriteEngine On
#RewriteCond %{SERVER_PORT} 80
RewriteRule ^s/(.+)$ s.php?id=$1 [L,QSA]
This would redirect s.php?id=1 to s/1.

To be clear from what you have above, the redirect is from
http://localhost/sell_script/s/1
to
http://localhost/sell_script/s.php?value=1
Not the other way around.
If your RewriteBase is /, you should include the sell_script in your rule.
RewriteRule ^sell_script/s/(.*)$ /sell_script/s.php?value=$1 [L,R=301]
Also note, I have added an L to make the rule an endpoint, and changed the rewrite to a 301 code which is more search-engine friendly, among other things.

Related

How to change htaccess rules to make urls better for seo?

I am experiencing some problems changing URLs with .htaccess.
I can change the rule for index.php but I can not change for any other page on my website.
Working example: http://example.com/novo_website/index.php - I successfully change the rule to http://example.com/novo_website/inicio/.
Non working example: http://example.com/novo_website/pedido_orcamento.php the rewrite rule is not working to http://example.com/novo_website/orcamento/.
The last example gives 404 error. Also, the 404 page redirection does not work also.
htaccess:
RewriteEngine On
RewriteBase /novo_website/
ErrorDocument 404 /novo_website/404_not_found.html
RewriteRule ^orcamento/?$ pedido_orcamento.php [NC,L]
RewriteRule ^registo/?$ registo.php [NC,L]
RewriteRule ^entrar/?$ area_empresa.php [NC,L]
RewriteRule ^inicio/?$ index.php [NC,L]
Can anyone help me resolve this situation?

.HTACCESS - Prevention of 404 with no URL query

I'm using .htaccess to re-write some of my URLs for my web application. The following is used to make the user page a little prettier when user_urls (practically a URL friendly username) are passed to it:
RewriteRule ^user/([0-9a-zA-Z-]+) /user?url=$1 [NC,QSA]
When I input the URL http://example.com/user, the page redirects just fine, and the PHP handles the lack of the query string. However, when the server receives the request of http://example.com/user/, it returns a 404 error.
How can I prevent this from happening?
Thanks.
Redirect to the correct URL by prepending a rule
RewriteRule ^(.*)/$ /$1 [L,R=301]
or append /? to your regexp.
You can use these rules in your .htaccess:
Options -MultiViews
RewriteEngine On
RewriteRule ^user/?$ user.php [NC,L]
RewriteRule ^user/([0-9a-zA-Z-]+)/?$ user.php?url=$1 [NC,QSA,L]

htaccess rewrite url folder/file to folder/file.php

I want ajax/file address to be redirected to ajax/file.php using .htaccess. I created a .htaccess file like below but that gives a 500 Internal Server Error.
RewriteEngine On
RewriteRule ^ajax/(.*)$ ajax/$1.php [L]
An additional information is that my website is working under a sub-folder. (localhost/myproject) RewriteRule ^ajax/(.*)$ /ajax/$1.php [L] redirects url to (localhost/ajax/file.php) instead of (localhost/myproject/ajax/file.php
Problem is that .* in your regex also matches file.php.
Use your rule like this:
Options -MultiViews
RewriteEngine On
RewriteRule ^ajax/([^./]+)/?$ ajax/$1.php [L]

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.

.htaccess rewrite redirect

I have some code which uses Rewrite engine in my .htaccess file and it specifies the directory for my SERPs. However when someone goes onto search/QUERY/ rather than search/QUERY/PAGE/ it displays a 404 error. Same with just search/.
I want it so that if someone just goes to search/QUERY/ that it redirects them to search/QUERY/1/ and for just search/ it redirects them to my homepage /. I have included a copy of my code below.
RewriteEngine on
RewriteRule ^search/([^/]+)/([^/]+)/?$ search.php?q=$1&category=web&d=$2
Can anyone help me with this problem?
Thanks in advance, Callum
Try this code in your htaccess :
RewriteEngine on
RewriteRule ^search/?$ / [R=301,L]
RewriteRule ^search/([^/]+)/?$ /search/$1/1/ [R=301,L]
RewriteRule ^search/([^/]+)/([0-9]+)/?$ search.php?q=$1&category=web&d=$2 [NC,L]
You can create multiple RewriteRule statements to accomplish this. (Make sure to omit the [R] flag on intermediate rules if you use it!)
The rule you posted will only rewrite if it's of the form /search/query/n/, so write a regular expression matching search/query that redirects to /search/query/1. Do the same to redirect home with no query.
Try these additional 2 rules in your .htaccess file:
RewriteRule ^search/([^/]+)/$ /search/$1/1/ [R=301,L]
RewriteRule ^search/?$ / [R=301,L]

Categories