I have this link:
index.php/forums/viewforum/5/
Now, I want that "forums" word in URL to become dynamic such that which ever word I replace it with, it still redirects to the same URL.
For example, if I have:
ProductA/viewforum/5/
it redirects to:
forums/viewforum/5/
For example, if I have:
ProductB/viewforum/13/
it redirects to:
forums/viewforum/13/
In other words, if there's a "view forum" word in the URL, it should trigger this rewrite.
I already have a .htaccess that removes the index.php from the URL so the rewrite rule should consider that too.
Is it possible?
HTACCESS:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule member http://%{HTTP_HOST}/404 [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^[^/]+/(viewforum/[0-9]+/?)$ /forums/$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(images|favicon\.ico|robots\.txt|index.php) [NC]
RewriteRule ^(.+)$ /index.php/$1? [L]
</IfModule>
Your 2nd and 3rd rules look suspect.
Have your full code like this:
RewriteEngine on
RewriteRule member http://%{HTTP_HOST}/404 [R=301,L]
RewriteCond %{REQUEST_URI} !^/forums/ [NC]
RewriteRule ^[^/]+/(viewforum/[0-9]+/?)$ /forums/$1 [L,NC,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(images|favicon\.ico|robots\.txt|index.php) [NC]
RewriteRule ^(.+)$ /index.php/$1 [L]
Related
I have been trying to apply a rewrite rule to change my urls which currently look like this:
www.mysite.com/my-site/en
change to:
www.my-site.com/en or /es
but I want it to be forceful and not allow someone to deliberately type in the latter.
Also
www.my-site.com/en/?page=car&category=15
change to
www.my-site.com/en/car/15 (or I can just put the category name instead)
I also have this:
www.my-site.com/en/?page=catalogue-item&category=204&id=563
which I want to change to:
www.my-site.com/en/563 (or I can just put in the item name instead)
Here is my htaccess so far:
# RewriteOptions inherit
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^my-site.com [NC]
RewriteRule ^(.*)$ http://www.my-site.com/$1 [L,R=301,NC]
RewriteRule ^$ my-site/ [L,NS,R=301]
RewriteCond %{HTTP_HOST} ^www.my-site.com/my\-site/ [NC]
RewriteRule ^(.*)$ http://www.my-site.com/$1 [L,R=301,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ my-site/$1 [L,NS]
RewriteRule ^([^/]*)$ /en/?page=$1 [L]
RewriteRule ^([^/]*)$ /es/?page=$1 [L]
RewriteRule ^([^/]*)/([^/]*)$ /en/?page=$1&category=$2 [L]
RewriteRule ^/([^/]+)/~([^/]+)/(.*)$ my-site/$1 [L,NS]
</IfModule>
I thank you in advance and hope someone could point out my problem, thank you again.
I'm trying to get a my original url which would have been properties.php?bed=1&page=1&perpage=12&area=testarea to /search/1/1/12/testarea. But the last query string 'area' is optional and so could be blank.
I have this so far below, the problem is if I add an extra ([^/]+)/ such and
RewriteRule search/([^/]+)/([^/]+)/([^/]+)/([^/]+)/? pages/properties.php?bed=$1&page=$2&perpage=$3&area=$4
and then don't supply an area in the url, it fails to work correctly. Is there a correct way to do this where the query sting can be optional?
Also another problem I'm having is I would like this
RewriteRule search-rooms/([^/]+)/([^/]+)/([^/]+)/? pages/rooms.php?bed=$1&page=$2&perpage=$3&area=$4
to be /search/rooms/ not /search-rooms/ but if I put an extra / after search it again doesn't work correctly and the page reads the wrong data from the URL.
This is the full version
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule property/([^/]+)/? pages/show.php?id=$1 [NC,L]
RewriteRule search/([^/]+)/([^/]+)/([^/]+)/? pages/properties.php?bed=$1&page=$2&perpage=$3&area=$4 [L,QSA,NC]
RewriteRule search-rooms/([^/]+)/([^/]+)/([^/]+)/? pages/rooms.php?bed=$1&page=$2&perpage=$3&area=$4 [L,QSA,NC]
RewriteCond %{HTTP_HOST} ^www.studentlettingsagency.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^studentlettingsagency.co.uk$
RewriteRule ^(.*) http://www.stla.co.uk/$1 [QSA,L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
</IfModule>
Any help would be great!
You can use the following rule :
RewriteCond %{REQUEST_URI} !^/search/rooms
RewriteRule search/([^/]+)/([^/]+)/([^/]+)/?([^/]*)?/?$ pages/properties.php?bed=$1&page=$2&perpage=$3&area=$4 [NC,L]
RewriteRule search/rooms/([^/]+)/([^/]+)/([^/]+)/?([^/]*)?/?$ pages/rooms.php?bed=$1&page=$2&perpage=$3&area=$4 [NC,L]
RewriteCond is important here ,as it tells the first rule not to match the uri /search/rooms
I currently have very little Apache experience, and am having difficulties with my .htaccess file. My question is this: how can I rename these files, listed below, properly? I believe my syntax is accurate, according to http://www.htaccesscheck.com, but when accessing these pages, either A: the page won't load due to a redirect loop, or B: the page won't load, but will redirect to the wrong page. Here is my current .htaccess file for this directory:
RewriteEngine On
RewriteBase /photos/
RewriteRule ^(.*)-(.*)$ archives.php?month=$1&year=$2 [L]
RewriteRule ^(.*)$ catpost.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]
Any help is much appreciated.
Try code below:
RewriteEngine on
RewriteBase /photos/
RewriteRule ^([0-9]{1,2})-([0-9]{4})$ archives.php?month=$1&year=$2 [L]
RewriteRule ^([0-9]+)$ catpost.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]
This rules will check, if:
request like yourdomain/11-1111, then return archives.php
request like yourdomain/111, then return catpost.php (you can type
any number)
else will return viewpost
You have some errors in your current .htaccess, because your second rule get result of first rule.
By the way, you can use http://htaccess.madewithlove.be/ to check step by step what is posted to your rewrite rules.
be sure to write a valid pattern
try this
RewriteEngine On
RewriteBase /photos/
RewriteRule ^(.*?)-(.*?)$ archives.php?month=$1&year=$2 [L]
RewriteRule ^(.*?)$ catpost.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*?)$ viewpost.php?id=$1 [QSA,L]
I'm using Rewrite Rule in htaccess to get page data by slug.
I'm doing it manually by adding lines to .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !www\.example\.co\.il$ [NC]
RewriteRule ^(.*)$ http://www.example.co.il/$1 [L,R=301]
ErrorDocument 404 /404.php
RewriteRule ^עמוד-מסוים$ /page.php?id=1 [L]
RewriteRule ^דוגמא-לעמוד$ /page.php?id=2 [L]
RewriteRule ^דוגמא-נוספת$ /page.php?id=3 [L]
RewriteRule ^טקסט-כלשהו$ /page.php?id=4 [L]
RewriteRule ^צרו-קשר$ /contact.php [L]
RewriteRule ^blog$ /blog.php [L]
It's work good but I need to add new .htaccess line every time.
my DB table has Id and Slug (and other info)
and my page.php use $_GET['id'] and then select the other data from DB by this ID.
I tried somthing like this:
.htaccess:
RewriteRule ^page/([^/]*)$ /page.php?id=$1 [L]
page.php:
$id=$_GET['id'];
if(is_int($id)==false){ // if slug was enterd
$result=$mysqli->query("SELECT `id` FROM `pages` WHERE `slug`='$id' limit 1");
while($row=mysqli_fetch_assoc($result)){
$id=$row['id'];
}//query
}
But the URL not look like I want (adding /page/ to url)
I tried that also:
.htaccess:
RewriteRule ^/([^/]*)$ /page.php?id=$1 [L]
but it's make problem with other url's on my site (that are not connected to page.php)
How can I make it work without adding htaccess line every time?
EDIT:
when I try this .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !www\.example\.co\.il$ [NC]
RewriteRule ^(.*)$ http://www.example.co.il/$1 [L,R=301]
ErrorDocument 404 /404.php
RewriteRule ^/?([^/]+)$ /page.php?id=$1 [L]
RewriteRule ^צרו-קשר$ /contact.php [L]
RewriteRule ^blog$ /blog.php [L]
I get error on all pages, If I moving up the RewriteRule ^/?([^/]+)$ /page.php?id=$1 [L] only the page.php file will work good (blog and contact will not work) and also non-WWW url's will not work good.
You could try adding some conditions, something like:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)$ /page.php?id=$1 [L]
to filter out all existing files and directories.
Also, you might want to consider sanitizing your $id parameter before you stick it in a query
Found the soultion. This working perfect:
ErrorDocument 404 /404.php
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^צרו-קשר$ /contact.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)$ /page.php?id=$1 [L]
I have dynamic URL website (irasol.com) while i navigate to menu the url shows like
http://irasol.com/index.php?id=1
I want url like this
domainname/home
domainname/aboutus
domainname/contactus
domainname/apply
home, aboutus, contactus, apply are menu name it is already in database.
my htaccess file is
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^/]*)\.php$ /index.php?id=$1 [L]
Use this instead:
Options -Multiviews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/?$ index.php?id=$1 [B,L]
Explanation
The first three conditions make sure that domainname/aboutus is not a real file, so that we don't rewrite files that already exist.
Options -Multiviews removes a number of potential problems
In your current code, get rid of the .php in your pattern:
RewriteRule ^([^/]+)$ /index.php?id=$1 [L]
You are not matching .php extensions in the request. You are only routing matches to a query string on a real .php extension
As for a better solution:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?id=$1 [L]