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

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?

Related

.htaccess simple rewrite rule not working in subdirectory

I'm working on a subdirectory, and want to rewrite ugly file-urls into prettier URLs.
Like:
domain.com/myfolder/myreport.php --> domain.com/myfolder/MyReport/
Here's what I have:
RewriteEngine On
RewriteRule ^test/?$ index.php [NC,L]
Taken from here
However, it's not working. I've tested every combination of regex I could imagine, nothing worked.
Are there any other potential problems? Is it possible that all rules are ignored/overwritten by a rule in the root directory or something?
The .htaccess IS being used, if I add "test", I get a 500 Error.
Can you just try the following,
RewriteEngine on
RewriteBase /myfolder/
RewriteRule ^(.*)$ /myfolder/index.php?/$1 [L]

Htaccess redirecting in the wrong way

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.

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]

404 page when no match found

i have this rewrite rules:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} \.php[\ /?].*HTTP/ [NC]
RewriteRule ^.*$ - [R=404,L]
RewriteRule ^(home|contact|about|submit|search)/?$ /index.php?page=$1 [L]
RewriteRule ^([a-z]+)/([a-z0-9-]+)/?$ /index.php?page=home&cat=$1&slug=$2 [L]
RewriteRule ^([a-z]+)/?$ /index.php?page=home&cat=$1 [L]
What i want is:
if user types something which is not matching last three conditions i want to show a 404 error. At present it loads home page. So i am check $_GET['page'] in there. I donot want to check in php.
do you have anything to improve my rewrite rules or any additions?
please help
Since everything is ultimately routing to index.php (Front-end Controller Pattern), I suggest dropping the the mod_rewrite and using:
FallbackResource /index.php
Then parse the URL in index.php and handle routing accordingly.
This will take some rewriting, but will be more flexible in the long run.
Read more about FallbackResource and Front-Controllers in PHP.
You may use mod rewrite to add argument to your web page index.php, then treating them in the php page possibly sending back a error page (as in cakePHP). that keeps only ONE rule.
RewriteRule ^([0-9A-Za-z]+)/?$ /index.php?argument=$1 [L]
Try to use ErrorDocumentwhit location page
ErrorDocument 404 /erreur_404.html

.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