I've created an htaccess file for my website that converts a URL like this http://www.example.com/fixture?id=1 to something like http://www.example.com/fixtures/1.
My htaccess file:
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^fixtures/(\d+)*$ fixture.php?id=$1
That works how I want it, but isn't exactly SEO friendly, so I'd like to pass in two more variables home and away like so http://www.example.com/fixture?id=1&home=Arsenal&away=Chelsea.
Then rewrite the URL to look like `http://www.example.com/fixtures/arsenal-vs-chelsea.
There are many questions about rewrite rules on SO, but none discuss adding a small portion of text between variables. How could I make it so that I could add the vs portion of the URL using a rewrite rule?
You can use:
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^fixtures/([^/]+)-vs-([^/]+)/?$ fixture.php?home=$1&away=$2
Rewrite to:
http://www.example.com/fixture?home=Arsenal&away=Chelsea
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 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
So I have my framework that handle all URLs via one index.php page. If I put site.com/profile/user it will go to site.com/index.php?url=profile&next=user and parameter "url" loads different pages for example /home would be index.php?url=home
Problem now is that I wont to expand my rewrite rules in htaccess file so I have one more additional parameter which would be for example site.com/profile/user/auctions and it would look like site.com/index.php?url=profile&next=user&category=auctions.
My rewrite rules are listed below:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^]+)/([^/]+)(/([^/]+))?$ index.php?url=$1&next=$2
So problem is I can't put additional "category" parameter which would be index.php?url=$1&next=$2&category=$3 , whenever I change rules one parameter works but other dont.
Thank you for reading, and I hope it could be solved.
Try this one:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /index.php?url=$1&next=$2&category=$3 [L]
I take this rule from my own .htaccess file and just rename variables, so it must work.
If the reason just in this file of course
I am very new to apache and mod re-write I am trying to make pretty links using .htaccess. My URL looks like this:
http://example.com/single_picture.php?name=Testing-123&cat_id=1
I want to make it look like
http://example.com/pictures/Testing-123/
That is just the name. Here is what I have put in my .htaccess file but it is not working.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^pictures/(\s+)*$ ./single_picture.php?name=$1&cat_id=$2
How do I do it?
Ahmar
Try This:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^pictures/([A-Za-z0-9\-_]+)/([A-Za-z0-9\-_]+)/?$ /single_picture.php?name=$1&cat_id=$2
Also remember that a rewrite will not rewrite the request a user has made. i.e. http://example.com/single_picture.php?name=Testing-123&cat_id=1 will work as will http://example.com/pictures/Testing-123/1/.
You will need to make the user view the page via the SEO link by changing the links on your website to reflect this.
I am looking for some help in my htaccess to better handle my urls when using a pagination function.
Currently my htaccess looks like this:
RewriteEngine On RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?alias=$1 [QSA,NC,L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?alias=$1/$2 [QSA,NC,L]
basically, I have two rules. At first I only had the second one, to direct all requests to my index page, where I then parse out some details of the url in order to show the correct content. I found that it didnt work for urls with only one sub directory, like example.com/bars, but only two directories like, example.com/bars/bar-name, so I added the rule before it.
This has been working great for me. All of my urls look great and the ?alias=bars/bars-name has been completlely hidden and now looks like /bars/bars-name
Recently though I have integrated a pagination php function. This function creates urls that looks like this:
example.com/bars?alias=bars&info=mysql_table_bars&page=2
Basically, 'info' is the table name that the pagination function queries, and 'page' is the current page. It seems 'alias' also started showing in the url once I started using this function.
What I would like is for my pagination to look like these examples:
example.com/bars/page/2
example.com/hotels/page/5
etc...
What do I need to add to my htaccess to handle this situation? Also, is there a way to simplify my existing two rules?
Regarding your original rules. The conditions that you have only get applied to the immediately following RewriteRule, so you need to duplicate them:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?alias=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?alias=$1/$2 [QSA,NC,L]
For the new pagination stuff, firstly, it is really bad to be pulling database table names from the query string, unless they've been validated somehow (which may be what's happening). But even then, it's still an information disclosure problem. So you can use these rules to always remove them:
# This matches the actual request when there's pagination in the query string
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/\?]+)\?alias=([^&\ ]+)&info=.*&page=([0-9]+)
# an optional sanity check, does the request URI match the "alias" param?
RewriteCond %1:%2:%3 ^(.*):\1:(.*)$
# redirect the browser to the URL with the table name removed
RewriteRule ^ /%1/page/%2? [L,R=301]
Then you need rules to internally rewrite them back to the request with the query string:
# rewrite it back to the query string
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)/page/([0-9]+) /$1?alias=$1&info=mysql_table_$1&page=$2 [L]