I have a website which contains friendly URL's like localhost/article/xyz instead of localhost/article.php?name=xyz. It all works fine, but i discovered that I still can access the site also by typing "localhost/article.php?name=xyz". Is there a way to redirect it automatically to friendly URL?
RewriteEngine on
RewriteRule ^article/([a-z,-]+)$ article.php?name=$1&
Just want to redirect that: "localhost/article.php?name=xyz" to "localhost/article/xyz".
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^article/(.*)$ article.php?q=$1 [QSA]
For more information on how to use mod_rewrite check out the doc
https://httpd.apache.org/docs/current/mod/mod_rewrite.html
Related
I ned to change this url
https://halopredictions.com/blog/index.php?url=german-bundesliga-prepare-to-return-on-9-may
to
https://halopredictions.com/blog/german-bundesliga-prepare-to-return-on-9-may
This is my .htaccess that am currently using
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ index.php?url=$1 [NC,L]
I have already enabled mod rewrite
The htaccess is on this path ...\blog\.htaccess
Any help I will highly appreciate
Before proceeding with the answer, if I understand correctly you want to redirect from https://halopredictions.com/blog/german-bundesliga-prepare-to-return-on-9-may To https://halopredictions.com/blog/index.php?url=german-bundesliga-prepare-to-return-on-9-may.
Since SEO Urls are used by the visitors, we have to convert them to actual path.
The below RewriteRule will be sufficient to achieve the above condition.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^blog/([A-Za-z0-9-\+]+)/?$ blog/index.php?url=$1 [NC,L,QSA]
Explanation:
^blog/([A-Za-z0-9-\+]+)/?$ - This part is what you want to rewrite from. Since, your URLs will be like in the pattern https://halopredictions.com/blog/german-bundesliga-prepare-to-return-on-9-may, we need to get the url part after blog/.
I have used a regular expression ([A-Za-z0-9-\+]+)/? to match the url part and it can be referrenced using $1.
Now we have to use $1 in our new url. For that, we have written the second part of RewriteRule where you can assign the referrence.
blog/index.php?url=$1 - Now, as you would assume we are using the $1 reference after index.php?url=, so that it will append it to the URL Query param and should lead to a valid path.
By the way, ^ this is used to indicate the start and $ for the end.
I am creating a rest api endpoint to input data into a database. I have been trying to rewrite the url into this format http://example.com/src/public/endpoint/data. The public in the url is a folder that had has an index.php file that all the urls will be routed to but it is not working. Below is my .htaccess code.
RewriteEngine
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://example.com/src/public/index.php?path=$1 [NC,L,QSA]
I am getting the link address http://example.com/src/public/?path=state/127&_=1579497796272 as the endpoint when i expect example.com/src/public/state/127. I know am not doing something correctly but i can not figure it out.
Try this :
RewriteEngine On
RewriteCond %{THE_REQUEST} \?path=(.*)&(.*)\sHTTP.*$
RewriteRule ^(.*)$ /src/public/%1? [L,R]
RewriteRule ^src/public/state/127 /src/public/?path=state/127&_=1579497796272 [L]
First you should add on after RewriteEngine which means enabling mod_rewrite .
The second line will captrue the request and get a part of query string.
Third line will externally redirect the request to be friendly.
The last line will redirect a new one, internally , to the correct path.
Note: if it is ok , change R to R=301 to be permenant redirection.
I have came accross a problem that every .htaccess query I've tried wasn't worked out ! I have URLs like this:
http://www.example.com/index.php?x=product
And I want to change it to a user friendly URL like this:
http://www.example.com/product/
Or it can be:
http://www.example.com/product.php
I've tried this code below:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^x=product$
RewriteRule ^index.php.*$ http://www.example.com/product.php? [R=302,L]
Now, it is redirecting perfectly, but this is not the problem. I've used this for only SEO so I must include http://www.example.com/index.php?x=product in the product.php file. Any help can be precious, thanks...
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /index\.php\?x=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)/?$ index.php?x=$1 [L,QSA]
This will redirect /index.php?x=product to /product/ and will rewrite it internally to /index.php?x=product in 2nd rule.
You don't need to put anything in the product.php file. Make sure there is a .htaccess in the directory that has the files you want to make url/seo friendly.
To have it SEO friendly make sure its in this format
http://www.example.com/product/ not http://www.example.com/product.php
if you must have a file extension, have it in http://www.example.com/products.html (you want to tell the search engine the page isn't dynamic although it is to get better pagerank)
Insert this in your .htaccess
RewriteRule /(.*)/$ products.php?x=$1
the (.*) pulls the elements out and puts them in variables $1
My .htaccess file currently looks something like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/([^/]*)/$ ?page=$1 [L]
It makes URLs friendly from something like domain.com/?page=2 to domain.com/page/2/.
However, is there a way that I can edit this .htaccess file to make it redirect URLs containing ?page=2 (or any page number) to it's nicely formatted URL (/page/2/) automatically? My current one just allows the friendly URL version to "exist", but in no way enforces a redirect to it.
Also, is there a way I can redirect ?page=1 or /page/1/ just to the main directory/home?
EDIT:
After using what I received as an answer from Jon Lin, I was able to solve the second part of my question. Considering that ?page=1 automatically redirected to /page/1/, all I had to do was redirect /page/1/ to the homepage:
RewriteCond %{THE_REQUEST} \ /+DIRECTORY/(?:page|)/1/?
RewriteRule ^ /DIRECTORY/? [L,R]
Add this right below the RewriteEngine on line:
RewriteCond %{THE_REQUEST} \ /+(?:index\.php|)\?page=([0-9]+)
RewriteRule ^ /page/%1/? [L,R]
I tried everything I see here in this site to rewrite dynamic url to SEO friendly URL.
Is it maybe because im using it in the localhost?
I try this but does not work also:
RewriteCond %{THE_REQUEST} ^GET\ /index\.php/?([^ ]*)
RewriteRule ^index\.php/?(.*) /$1 [R,L]
RewriteCond $0 !^index\.php($|/)
RewriteRule .* index.php/$0 [L]
I also refer to an online dynamic url generator , but it doesn't work either. Please help.
I would like to rewrite these couple of URLs:
index.php?p=home
index.php?p=about me
index.php?p=contact me
You can use this one:
RewriteEngine on
RewriteBase /
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1
Then on your index.php page, all the variables will put accessible via $_GET['q']. If you further use $location=array_filter(explode('/',$_GET['q'])); then $location will be an array containing each directory, so www.mysite.com/firstdir/seconddir/thirddir will have $location[0] as 'firstdir' and $location[1] as 'seconddir' and so on. You could then compare these to url_aliases in your database to determine what content/template to display.
This also works for localhost for me, except I change the base url part from "/" to "/mycurrentconstructionsite/"
Hope that helps!