Rewrite all url without page name - .htaccess

I have a website that has lots of pages, and i want to rewrite url for all the pages without page name,
for example:
http://example.com?category.php?cat_name=mobile
It should become like this.
http://example.com/mumbai/mobile
dubai will be a dynamic value which will keep changed by php.
We have another one the same like this.
http://example.com?blog.php?blog_name=restaurants
http://example.com/delhi/restaurants
I have lots of pages like these mentioned above but url should hold the value of city name and page name should not be there.. Blog page will redirect to blog page and category will redirect to category page only.
I did try like this.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)$ category.php?cat_name=$1 [QSA,NC,L]
But it didnt work because all redirections will only hit to the first url category.php and the same page only.

Related

Redirect all pages to the index page

I have a temporary static HTML page. But there are a bunch of URL's added to an other website. All these URL's go to page a that doesn't exist at the moment. So it needs to be redirected to the index page.
So every page needs to be redirected to https://experienceantwerp.be
Example page that doesn't exist at the moment: https://experienceantwerp.be/nl/aanbod/gidsenwandelingen/geschiedenis/voetgangerstunnel
When you go to that URL, the styles are gone.
My current .htaccess file is
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.html/$1 [L]
When I try this, for example: https://experienceantwerp.be/hfhfhf it seems to work. I guess if there is one slash in the URL, it works.
Update: I've found a solution, but it doesn't feel right. When I add the CSS and JS file with there absolute path, it works.

Mod_Rewrite for URL with multiple variables | .htaccess

I develop an e-shop site with products so for this proposal i made a product page for each of products with Mod_Rewrite for URL with the code above in my .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*/([a-zA-Z0-9_-]+)|([a-zA-Z0-9_-]+))$ ./product.php?name=$1 [L]
So when user clicks on the link www.example.com/Nike-Air goes to Nike-Air shoes page and etc.
What i would like to do is to add one parameter more in the url, let's say size
RewriteRule ^(.*/([a-zA-Z0-9_-]+)|([a-zA-Z0-9_-]+))$ ./product.php?name=$1&size=$2 [L]
but this parameter to not append in the url, still to displays www.example.com/Nike-Air
and also to be able to get the size data from $_GET method.
Is it possible?
Any ideas guys?

rewrite url and get variable

How can I get the second part of a url to be a variable, rewrite it, and provide a fancy URL.
So example www.mywebsite.com/AFL would rewrite to www.mywebsite.com/index.php?league=AFL but would still display as www.mywebsite.com/AFL.
I am using the same pages for multiple leagues. When a user signs up they establish a league name in the database and then on each page I look for the league variable to display the info.
I also have many different pages. So a user could go to www.mywebsite/AFL/schedule_teamdisplay.php?Team=ABC&Year=2014. This would rewrite to www.mywebsite.com/schedule_teamdisplay.php?league=ALF&Team=ABC&Year=2014.
The league will always be the second entry in the URL.
Thanks
You can put this code in your htaccess (which has to be in your root folder)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /index.php?league=$1 [L]
RewriteCond %{DOCUMENT_ROOT}/$2 -f
RewriteRule ^([^/]+)/([^/]+)$ /$2?league=$1 [L,QSA]

Wordpress custom listing page

I have WordPress site that has films and actor custom post types. Also I'm creating relationship with films and actors.
I have a page to list films that related to actor. On this page visitors can see in which films actor take role.
This pages URL is like this:
http://www.mywebpage.com/actors-films/?id=123456
id refers to the actor, with this id I'm doing a query and showing it on the page.
also this page supports pagination, this URL works too:
http://www.mywebpage.com/actors-films/page/2/?id=123456
actors-film page is a page that uses a custom template, this template determines the actor id and shows his films.
Every thing works fine, but I want to change the URL a little bit SEO friendly.
I have tried many URL formats
http://www.mywebpage.com/actors-films/jim-carrey
I don't know why but this URL redirects to actors page.
http://www.mywebpage.com/actors/jim-carrey
http://www.mywebpage.com/actors-films/123456-jim-carrey
I have tried to catch this kind of URL with .htaccess but no way to catch it, it always shows a 404 not found.
So, I need to do a rewrite with pagination support, can you please suggest me a way to do this?
edit : here is my .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^actors-films/([0-9]*)\-([^/]*)$ ^actors-films/?id=$1 [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Edit : I have realized that not rules works for me :
example :
http://www.mywebsite.com/tex shows a 404 with this rule : RewriteRule ^/tex /index.php [L]
Here is the solution, create a page named
add_filter('query_vars', 'ap_register_query_vars');
function ap_url_redirect_rules(){
add_rewrite_rule('actors-films/([^/]*)','index.php?pagename=artist-film-list&actor_slug=$matches[1]','top');
flush_rewrite_rules();
}
function ap_register_query_vars($query_vars){
$query_vars[]='actor_slug';
return $query_vars;
}
Now we can call the url http://mywebsite.com/actors-films/john-doe

Rewrite rule to drop .html and to keep the querystring while navigating between pages

I have html pages and I want to remove the .html and keep the query-string in place that when a visitor comes in with a query-string and navigate between pages the query-string remains in place.
So far getting the .html to go is fine, but I cannot get the query-string to stay between pages. I don't particularly want to introduce php into my pages, if its possible with a rewrite rule that will be great.
As follows:
Visitor comes in on homepage www.example.com?ID=123
When visitor clicks on the link www.example.com/page1.html , I want the visitor to go to www.example.com/page1?ID=123
My code currently is as follows:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html
Thanks

Resources