I've been through plenty of docs but can't find the right rule on this case.
I have a single index.php file, containing a single iframe.php.
There is 3 optional parameters, although the first two have defaults if not specified. The parameters are passed to the iframe.
So I have
domain.com/?lang=en&country=us&game=myId
The iframe is called within the index like so :
<iframe src="iframe.php?lang=en&country=us&game=myId">
I would like the URL to be rewritten as :
domain.com/en-us/myId
without messing up the iframe parameters.
But
If the page is called without language parameters, the defaults are still set in the iframe, calling
domain.com
gives
<iframe src="iframe.php?lang=en&country=us">
In this case I would like the main URL to display
domain.com/en-us
Try something like this in the htaccess file in your document root:
RewriteEngine On
RewriteRule ^$ /en-us [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]{2})-([a-z]{2})(?:/(.*)|)$ /iframe.php?lang=$1&country=$2&game=$3 [L,QSA]
You then need to make sure you are constructing your URL's like:
domain.com/en-us/myId
and not use the query strings.
If you need to redirect the browser when accessing pages using query strings, include:
RewriteCond %{THE_REQUEST} \ /iframe.php\?lang=([a-z]{2})&country=([a-z]{2})(?:&([^&\ ]+))($|&|\ )
RewriteRule ^ /%1-%2/%3 [L,R=301]
To create separate rules for 2 and 3 parameters:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]{2})-([a-z]{2})/(.+)$ /iframe.php?lang=$1&country=$2&game=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]{2})-([a-z]{2})/?$ /iframe.php?lang=$1&country=$2 [L,QSA]
Related
I would like to show certain content on my website. I choose the content based on a URL parameter, like page=about. This works well, and my URL looks like example.com/mysite/index.php?page=about. My issue is that I would like my URL to read example.com/mysite/about, where the last part is the name of the page parameter.
I am attempting to do this through htaccess. This is my exact code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
I see no change on my page nor to the URL structure. The URL still reads ...index.php?page=about. Am I doing something wrong? I tried this a bunch of different ways and I still get the same result. I also tried simply doing this, with no change:
RewriteEngine On
RewriteRule ^([a-z]+)\/([0-9]+)\/?$ index.php?page=$1 [NC]
I didn't think it was working, so I tried a redirect to some other site and it worked. Therefore, the htaccess file is getting read and able to function.
Any assistance would be helpful!
Thank you,
BP
You may use these 2 rules in your site root .htaccess:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+(mysite)/index\.php\?page=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/mysite/([^/]+)/?$ [NC]
RewriteRule ^ index.php?page=%1 [L,QSA]
I have this url:
www.mywebsite.com/news/best-ever-phones
The part best-ever-phones is variable and could be anything.
index.php file is checking database for article with url best-ever-phones
The problem is that in ./news/.htaccess file I have this code:
RewriteEngine On
RewriteBase /news/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?article=$1 [L,QSA]
which is supposed to load index.php file but server returns error 404 Not found when I visit www.mywebsite.com/news/best-ever-phones url.
What is wrong?
Try this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?article=$1 [L]
I think the problem may be with Rewrite base and your relative path. But this is how i'ld do it.
<IfModule mod_rewrite.c>
Options -Indexes
RewriteEngine On
RewriteRule ^news/([A-Za-z-.]+)/?$ news.php?article=$1 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>
This should fix you up, rename your index to news.php
Perhaps give this a shot:
RewriteEngine On
RewriteBase /news/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-\/]+)\/?$ index.php?article=$1 [L,QSA]
This is presuming that you want the / character to appear in your $article variable, so you can logically separate different parts of the friendly URI if you want to in /news/index.php. Otherwise, single segment friendly URIs like /news-item-1 or whatnot can end with a trailing slash or not.
Really you don't want trailing slashes, as it might result in duplicate content penalties for loading the same page with a different URL (the / counts), so you could either not match \/? outside of the capture group or 301 redirect in index.php to the URL with the trailing slash removed. This way you also have clean query arguments, e.g. /news/something-big?orly=yes instead of /news/something-big/?orly=yes
I am using this site to test the rewrites. Otherwise untested as we don't have your code handy, so no way to tell whether it would work or not in your environment.
This code is used when we click on any url it will hide its extension.
But If you want to goto index.php page then write
echo 'Index Page ';
?>
I have the two following URL like below
www.site.com/index.php?key=blah
www.site.com/index.php?key=blah&pass=blahblah
I want to make it like the following using .htaccess
www.site.com/blah/blahblah
www.site.com/blah
in same webpage
How can i achieve this using .htaccess?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ index.php?key=$1&pass=$2
should do it.
Edit: If you want the parameters optional, use this one.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /index.php?key=$1
RewriteRule ^([^/]+)/(([^/]+)?)$ /index.php?key=$1&pass=$2
I simply added a new rule where only one parameter is required. It does match first when only one parameter is given. If two parameters a given the second rule matches only.
I am trying to implement URL rewriting in my PHP application. Can someone share a step by step procedure of implementing URL rewriting in .htaccess.
In my application I want to implement following URL
www.domain.com/shop/shop.php?shopname=myshop&sh=1
to
www.domain.com/shop/myshop
i use the follwing
RewriteEngine On
RewriteBase /shop/
RewriteCond %{THE_REQUEST} /shop\.php\?shopname=([^\s&]+)&sh=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ shop.php?shopname=$1&sh=$2 [L,QSA,NC]*
It shows the url like www.domain.com/shop/myshop
But not pointing to www.domain.com/shop/shop.php?shopname=myshop&sh=1
If I understand you correctly, you can do what you want with:
RewriteCond %{QUERY_STRING} ^id=([^&]*)&name=(.*)$
RewriteRule ^/shop/shop.php$ /shop/$2?id=$1
The RewriteCond check for a query string of the correct format and extracts the values. $1 = the ID and $2 = the name.
The RewriteRule rewrites a URL of the correct format to the new format, inserting the name and id parameters from the RewriteCond.
I'm trying to create shorter urls to some of my pages.
Previously I had a system that URLs were like /index.php?m=page&title=Page-Title
The piece of code I have now allows me to remove the everything 'm=' part. /index.php?m=page in here, the 'page' is a name of different modules, so this part might still change. I want to change the url only from 'page'.
How would I rewrite this so that my URL would be composed only as such:
/page=Page-Title
Would that be even possible since I'm using $_GET in 2 places?
My current .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/?p=$1 [L]
You can have your rules like this:
RewriteEngine On
RewriteRule ^page=([^/]+)/?$ /index.php?p=page&t=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/?p=$1 [L,QSA]