Friendly Url for .php?id=1 - .htaccess

I have this url
www.mysite.com/proyecto.php?id=3
and want this type of url
www.mysite.com/proyecto/id/3
I remember using this kind of code but is not working, any idea please?
<ifModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteRule proyecto/id/(.*)/ proyecto.php?id=$1
RewriteRule proyecto/id/(.*) proyecto.php?id=$1
RewriteRule ^([^\.]+)$ $1.php [NC,L]
ErrorDocument 404 /error404.php
</ifModule>
Finally I improved a bit and I did www.mysite.com/proyecto/1 width the following code in the .htaccess
Thanks for the help!
RewriteEngine On
RewriteRule ^proyecto/([0-9]+)/?$ proyecto.php?id=$1 [NC,L]

Your code sample looks very confusing and does not match your verbal description at all. If I got you right, this is main part you are looking for:
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$
RewriteCond %{REQUEST_URI} !^proyecto/id/(\d+)/? [NC]
RewriteCond %{QUERY_STRING} ^id=(\d+) [NC]
RewriteRule ^.*$ proyecto/id/%1/? [NC,L,R=301]

Related

htaccess rewrite rule, url doesn't work when i add a / at the end and it show error

Options +FollowSymLinks -MultiViews -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
DirectoryIndex index.php
RewriteCond %{HTTP_HOST} ^themobilesapp.com$ [NC]
RewriteRule (.*) https://www.themobilesapp.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^([A-Za-z0-9-]+)$ specification.php?url=$1 [L]
ErrorDocument 404 404error.php
</IfModule>**strong text**
When I open the URL https://www.themobilesapp.com/Motorola-Moto-G4-Play-specifications-7443
it works fine but when i tried to open the URL by adding "/" in the last on the url like https://www.themobilesapp.com/Motorola-Moto-G4-Play-specifications-7443/
I have tried many but it doesn't solve my problem.
I want my URL as https://www.themobilesapp.com/Motorola-Moto-G4-Play-specifications-7443
With your shown samples, please try following Rules in your .htaccess file. Please make sure to clear your browser cache before testing your URLs.
Options +FollowSymLinks -MultiViews -Indexes
DirectorySlash Off
<IfModule mod_rewrite.c>
RewriteEngine On
DirectoryIndex index.php
RewriteCond %{HTTP_HOST} ^themobilesapp\.com$ [NC]
RewriteRule ^(.*)$ https://www.themobilesapp.com/$1 [NE,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)/?$ $1.php
RewriteRule ^([\w-]+)$ specification.php?url=$1 [L]
ErrorDocument 404 404error.php
</IfModule>
Also I saw in your previous edit that you need rule like rewriting to given php files with uri, you could cover all those rules with following single rule here:
RewriteRule ^([^/]*)/([\w-]+)/?$ $1.php?url=$2 [L]
This problem has been solved using the below code.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

Friendly URL's returns 404

Hello everyone.
I have a problem with my htaccess file. I made friendly url's but server returns 404 error and i can't find where i made mistake in code. I spent 3 hours on it and still nothing. I'm begging you for help guys :D
Here's my code:
Options -Indexes
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9-_/]+)$ /$1.php [L,NC,QSA]
RewriteRule ^([a-zA-Z0-9-_/]+).(php|html|htm)$ /$1 [NC,R=301,L]
RewriteRule ^index.(php|html|htm)$ / [R=301,L]
Redirect is making good but server returns 404 error instead display website.
I want to make urls like example below:
From:
www.domain.com/customers.php
www.domain.com/products/number1.php
To:
www.domain.com/customers
www.domain.com/products/number1
Ok i solved this but now i have a redirect loop :(
New code:
Options -Indexes
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]
RewriteRule ^([a-zA-Z0-9-_/]+).(php|html|htm)$ /$1 [NC,R=301,L]
RewriteRule ^index.(php|html|htm)$ / [R=301,L]
Try this
RewriteOptions inherit
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

Add a specific part to url using .htaccess

I guess it's an easy question but I hardly know syntax used in .htaccess, so I got a bit stuck. How to add '/de/' after a domain name in this case, so the result will be something like 'blablabla.com/de/blog' or 'blablabla.com/de/offices' instead of 'blablabla.com/blog' and 'blablabla.com/offices'?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_COOKIE} lang=de [NC]
</IfModule>
I know that there should be RewriteRule after RewriteCond but, as I've said before, I'm not familiar with the syntax...
You can use:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_COOKIE} lang=de [NC]
RewriteCond %{REQUEST_URI} !^/de/ [NC]
RewriteRule ^(.*)$ /de/$1 [R,L]
</IfModule>
To access your site urls with /de segment ,you can use :
RewriteEngine On
RewriteBase /
RewriteRule ^de/(.+)$ /$1 [NC,L]

removing url parameters from one url using .htaccess codes

I tried lot of combinations using available examples (.htaccess directives), but I couldn't able to achieve what I want. The problem is...
My actual URL is
http://localhost/smon2/custsms/index.php?p_mob=9886419001
What I want is
http://localhost/smon2/custsms/
Please help me in wrinting .htaccess code lines.
Thanks
I tried with few of the items below...
RewriteEngine On
RewriteCond %{QUERY_STRING} ^p_mob=1$
RewriteRule (.*) $1? [R=permanent]
RewriteEngine On
RewriteCond %{QUERY_STRING} "p_mob=" [NC]
RewriteRule (.*) /$1? [R=301,L]
RewriteEngine On
RewriteRule ^/custsms/%{QUERY_STRING}/?$ http : //localhost/smon2/custsms/ [NC,L]
Nothing was working. please help.
Replace all your current rules with this rule:
DirectoryIndex index.php
RewriteEngine On
RewriteBase /smon2/
RewriteCond %{QUERY_STRING} ^p_mob=\d+ [NC]
RewriteRule ^(custsms)/index\.php$ $1/? [R=301,L,NC]

Issue with subdomains and htaccess for SEO

I would like to change urls from:
http://subdomain.domain.com/page/ to http://subdomain.domain.com/?page=pagename
and also:
http://domain.com/page/ to http://domain.com/?page=pagename
though haven't had much success.
Here is my htaccess file so far [updated]
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
# Remove 'www'
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# Add slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://resolutiongaming.com/$1/ [L,R=301]
# Subdomain redirect
RewriteRule ^/(webdev)/(.*)$ http://webdev.resolutiongaming.com/$1 [R=301,L]
RewriteRule ^/(artwork)/(.*)$ http://artwork.resolutiongaming.com/$1 [R=301,L]
RewriteRule ^/(music)/(.*)$ http://music.resolutiongaming.com/$1 [R=301,L]
# Search engine optimization
RewriteRule ([a-zA-Z]+)/$ ?page=$1
I used RewriteRule ^([a-zA-Z]+)/$ ?page=$1 which seemed to work for the domain url but not the subdomain. Any help would be appreciated.
Actually had to do something like this recently. Try this for your Subdomain redirect block:
RewriteRule ^/(webdev)/(.*)$ http://webdev.resolutiongaming.com/$2 [R=301,L]
RewriteRule ^/(artwork)/(.*)$ http://artwork.resolutiongaming.com/$2 [R=301,L]
RewriteRule ^/(music)/(.*)$ http://music.resolutiongaming.com/$2 [R=301,L]
Or maybe this; note the change from $2 to $1:
RewriteRule ^/(webdev)/(.*)$ http://webdev.resolutiongaming.com/$1 [R=301,L]
RewriteRule ^/(artwork)/(.*)$ http://artwork.resolutiongaming.com/$1 [R=301,L]
RewriteRule ^/(music)/(.*)$ http://music.resolutiongaming.com/$1 [R=301,L]
EDIT: Or Maybe try this. Note that you need to capture two parts of the URL to rewrite as you are explaining. Each item in parenthesis ( and ) corresponds to a string in the rewrite. Try this. Using your nicer regular expression RewriteRule as you mention in the comments:
RewriteRule ^([a-zA-Z]+)/(.*)$ /$1/$2 [R=301,L]
Or maybe this:
RewriteRule ^([a-zA-Z]+)/(.*)$ /$1/page=$2 [R=301,L]
So it was an easy fix really. I basically put a .htaccess file in each subdomain directory that looks like this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
# Search engine optimization
RewriteRule ([a-zA-Z]+)/$ ?page=$1
Hope this helps someone. :D

Resources