How to Change url format using htaccess - apache

I need your help I am trying to change my URL format but I cannot do that I keep getting Object Not Found error.
Real URL is : http://localhost/website/page.php?pid=about
I want like this url using htaccess http://localhost/website/about.php
My code is:
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteCond %{HTTP_HOST} !^www\.
#RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php !-f
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([a-zA-z0-9\_\-\+]+))\.html|([a-zA-z0-9\_\-]+)\.php$ page.php?pptype=$2&pppage=$3&id=$4 [QSA,NC,L]
</IfModule>

Try it like this for your url similar to http://localhost/website/about.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewrwiteRule ^([\w-]+)/([\w-]+).php$ $1/page.php?pid=$2 [L]

Use this in your .htaccess:
RewriteEngine On
RewriteRule ^website/([^/]*)\.php$ /website/page.php?pid=$1 [L]
It will leave you with the URL: http://localhost/website/about.php

Related

How rewrite url in htaccess

Good Morning,
I have documents within the structure of my website, which are accessed with the following url format:
http://example.com/docs/files/123/mydoc.pdf
http://example.com/docs/files/475/otherdoc.pdf
I want when a url of the above type is accessed, it is renamed with the following format:
http://example.com/123/mydoc
http://example.com/475/otherdoc
I have the following rules in the htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteRule ^/docs/files/([0-9]+)/([a-zA-Z0-9-]+)\.([a-zA-Z-]+) /$1/$2 [QSA,L]
</IfModule>
If I type in the browser
http://example.com/docs/files/123/mydoc.pdf
It doesn't change the url to
http://example.com/123/mydoc
What am I doing wrong?
Thanks
You may try these rules in your site root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+files/(\S+)\.pdf[?\s] [NC]
RewriteRule ^ /%1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/files/$1.pdf -f
RewriteRule ^(.+?)/?$ files/$1.pdf [L]
With your shown samples, could you please try following. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s(/files/.*)\.pdf\s [NC]
RewriteRule ^ http://%{HTTP_HOST}%1 [R=301,NE]
RewriteRule ^(.*)/?$ files/$1.pdf [L]

Renaming file in URL (.php file)

I have a file called filename-step1.php and looking to make the url domain.com/filename/step1 through .htaccess.
My current .htaccess file looks like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
At the moment, this works fine for removing the .php tag from the url. Just need to fix the directories.
You could add a RewriteRule to redirect to $1/$2.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ /$1-$2.php [NC,L]
</IfModule>
So, filename/step1 will trigger the filename-step1.php file.
I am giving 2 solutions here, please try one at a time only in your .htaccess file. With your shown samples, could you please try following as a Generic rules.
RewriteEngine ON
RewriteCond %{HTTP_HOST} domain\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/(.*)/?$ $1-$2.php [NC,L]
OR in case you are looking for specifically url filename/step1 try following.
RewriteEngine ON
RewriteCond %{HTTP_HOST} domain\.com [NC]
RewriteRule ^(filename)/(step1)/?$ $1-$2.php [NC,L]

Simple url Htaccess Redirection + youtube

Need help in forwarding below url with .htaccess rules
https://www.example.com/watch?v=yzwRsQ7rR-o
forward to
https://www.example.com/?url=https://www.youtube.com/watch?v=yzwRsQ7rR-o
I've following exisitng rules
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Check this redirect with query string
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} ^watch
RewriteCond %{QUERY_STRING} v=([A-Za-z0-9-]+) [NC]
RewriteRule (.*) /?url=https://www.youtube.com/watch?v=$1 [R=301,L]
</IfModule>

url rewriting synthax with .htaccess

I am on apache 2 and I would know if my htacces is correct
my url are for example :
localhost/test/boutique/index.php
localhost/test/boutique/index.php/language,en
localhost/test/boutique/index.php/Products/Description/products_id,1
localhost/test/boutique/index.php/Products/Description/products_id,2/language,fr
What is the best approach for a good url like above
In suppose index.php must deseapear to hav someting like that
localhost/test/boutique/Products/Description/products_id,1
I try this but it does'nt work
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://localhost/test/boutique/index.php/$1 [PT,L]
Assuming /test/boutique/ is a real directory, you can use these rules inside /test/boutique/.htaccess:
RewriteEngine On
RewriteBase /test/boutique/
# remove index.php if entered directly by clients
RewriteCond %{THE_REQUEST} /index\.php/(\S*)\s [NC]
RewriteRule ^ %1 [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!index\.php$).* index.php/$0 [L,NC]

How can i redirect one url to another url with htaccess file?

I want redirect from domain.com/temp/xyx/tech-specs to domain.com/temp/xyx/tech-specs/pemf-ts through htaccess. I tried below code but it is not redirecting
RewriteEngine On
RewriteBase /temp/xyx/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^tech-specs$
RewriteRule ^$ tech-specs/pemf-ts [L,R=301]
You are using HTTP_HOST variable but what you need to match is REQUEST_URI. Also keep redirect rule before internal rewrite rule.
Have it like this:
RewriteEngine On
RewriteBase /temp/xyx/
RewriteRule ^(tech-specs)/?$ $1/pemf-ts [L,R=301,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?_route_=$1 [L,QSA]