How to rewrite dynamic URL to SEO Friendly URL? - php

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!

Related

Ubuntu apache htaccess custom url for seo friendly not working

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.

htaccess - redirect GET parameters

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

URL rewriting in .htaccess doesn't works as it should

I am currently coding my own website in PHP and MySQL on WampServer (local). I've already rewrited some of my URLs successfully. But I'm having problems with one of them.
I want to display on a page all my articles listed in a category using the GET method. For the moment, I have the following URL : http://localhost/actuco/cat.php?id=xpS3cc&slug=amerique-du-nord and I would like to use and display this URL as http://localhost/actuco/c-xpS3cc-amerique-du-nord/ (which does contains exactly the same GET parameters than the original URL).
I have tried to do this with the following line in my .htaccess file
RewriteRule ^c-([^/]*)-([^/]*)/$ cat.php?id=$1&slug=$2
When I write the second URL in my browser, it shows me a blank page with no code lines at all. My first URL works perfectly.
I'm really lost and I really don't know how to fix it.
Here is the whole .htaccess file used on my website (all other URL rewritings in this file do work).
Options +FollowSymlinks
RewriteEngine On
RewriteBase /actuco/
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_URI} /+[^\.]+$
#RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/$ article.php?lng=$1&yr=$2&mo=$3&dy=$4&slug=$5&total_slug=$6
#RewriteRule ^([^/]*)-([^/]*)-([^/]*)$ waluty.php?cur=$1&amt=$2&lang=$3
RewriteRule ^([^/]*)/([^/]*)$ url.php?mode=$1&u=$2
RewriteRule ^c-([^/]*)-([^/]*)/$ cat.php?id=$1&slug=$2
RewriteRule ^bio$ o.php [L]
Thanks in advance for your help !
just pest this code in your .htaccess file
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?/$1 [L,QSA]
hopefully, it will work.
Solved ! Just forgotten to add slash before $ in this line
Before :
RewriteRule ^([^/]*)/([^/]*)$ url.php?mode=$1&u=$2
After :
RewriteRule ^([^/]*)/([^/]*)/$ url.php?mode=$1&u=$2
Now it works, but I still have a problem with multiple hyphens in slug described here : Multiple hyphen handling in .htaccess (URL rewriting)

Redirecting existing URLs to SEO Friendly URLs

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

How exactly does mod_rewrite work when displaying old url

I have used mod_rewrite to change http://mywebsite.com/?page=2 to http://mywebsite.com/page/2 using the following code:
RewriteEngine On
RewriteRule ^page/([^/]*)\.php$ /?page=$1 [L]
But when i type in the old url - http://mywebsite.com/?page=2 the page still appears and the url doesnt change to the static one. Is mod-rewrite meant to redirect the user or just make everything on the dynamic url appear on the static one when entered? If so how can i redirect any user that goes to the old dynamic url - http://mywebsite.com/?page=2 to the new static one http://mywebsite.com/page/2? Plus does google still index the old url?
This is my complete .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# external redirect from actual URL to pretty one
RewriteCond \s/+\?page=([^\s&]+) [NC]
RewriteRule ^ /page/%1? [R=301,L]
# existing rule
RewriteRule ^page/([^/]+)/?$ /?page=$1 [L,QSA]
Have your full .htaccess like this:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+\?page=([^\s&]+) [NC]
RewriteRule ^ /page/%1? [R=301,L]
# existing rule
RewriteRule ^page/([^/]+)/?$ /?page=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [L]
mod_rewrite necessarily will not redirect the visitor to the new fancy URL, but it will make sure that anybody who visits the new URL will still access it as if it is using the old URL
I believe you can achieve something like that with mod_substitute, but that's not something I will advice you to do now.
If you can, manually change the links to reflect the new changes, or have a function which will parse out your links such as
http://mywebsite.com/?page=2 to http://mywebsite.com/page/2
such as
function fancyuri($url){
//blah blah, strip out ? and change all occurence of equality sign = to forward slash
//do some other tricks
//return formatted links
}
And then pass your links as a param to the function and have it return your nice urls, in this case, even if you don't want it again, you can strip off the formatting codes in the function and still have ur original url returned.

Categories