I'm having an issue trying to get my htaccess to work properly
I'm custom building my website, but have not figured out how to get the blog posts to show right.
My base URL is working for the pages. Example: mywebsite.com/contact
But I'm trying to get the blog posts to work like this. mywebsite.com/posts/post-title-here
Here is my htaccess, and I've commented out methods I've tried. But not fully understanding what I'm doing wrong. Can someone point me in the right direction?
#Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*) index.php [QSA,NC,L]
#RewriteRule ^posts/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ view-post.php?id=$2
#RewriteRule ^posts/([A-Za-z0-9-]+)/?$ view-post.php?slug=$1 [NC,L]
#RewriteRule ^posts/(.*)$ view-post.php?slug=$1
#RewriteRule ^posts/(.+)$ view-post.php?title=$1 [L]
Options -Indexes
Post this above your index.php line in .htaccess
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^/?posts/(.*) view-blog.php [NC,QSA,L]
You can try these on your htaccess file:
RewriteRule ^posts/([^/]*)$ view-post.php?slug=$1 [QSA,L]
RewriteRule ^posts/([^/]*)$ view-post.php?title=$1 [QSA,L]
Related
I have this htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
DirectoryIndex index.php
RewriteRule ^doit$ doit.php [L]
RewriteRule ^([\$\.A-Za-z0-9_-]+)$ follow.php?follow=$1 [QSA,L]
But when i try to go in the index page, everytime showing follow.php page, even doit page show this last one.
Any fix for this issue please ?
Thank's.
RewriteCond technically applies only to the first RewriteRule, so
please try using something like this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([\$\.A-Za-z0-9_-]+)$ follow.php?follow=$1 [QSA,L]
#RewriteConds for this rule
RewriteRule ^doit$ doit.php [L]
DirectoryIndex index.php
I hope be useful. Regards.
I'm need some help with an htaccess rewrite. I would like to do a clean url setup that is dynamic.
For example
mydomain.com/pagename/var1/var2/
mydomain.com/pagename2/var1/var2/var3/etc
mydomain.com/pagename3/var1/var2/var3/etc
I would like the first level to be alway be the filename in the rewrite?
mydomain.com/pagename.php?v1=var1&v2=var2&v3=var3
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$0\.php -f [NC]
RewriteRule ^([^.]+)/?$ $0.php?v1=$1&v2=$2&v3=$v3 [L]
This will do your job done,
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^home/{0,1}$ pagename.php?v1=1[QSA,L]
RewriteRule ^sone/{0,1}$ pagename.php?v1=2[QSA,L]
RewriteRule ^music/{0,1}$ pagename.php?v1=3[QSA,L]
RewriteRule ^video/{0,1}$ pagename.php?v1=3[QSA,L]
</IfModule>
and the link look like that: mydomain.com/home but the real link is mydomain.com/pagename.php?v1=1
I want to use two RewriteRules to redirect Pages and Users to the right link but I'm coundn't achieve what I want, maybe someone could help me...
I want to redirect any Url Friendly page from:
http://www.example.com/my-url-friendly-page
to
http://www.example.com/index.php?cod=my-url-friendly-page
AND User Profiles from:
http://www.example.com/profile/username
to
http://www.example.com/profile/index.php?user=username
This is my htaccess, the first rule is working ok but I couldn't fix the second one...
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?cod=$1
RewriteRule ^/profile/(.*)$ index.php?user=$1 [QSA,L]
</IfModule>
Can anyone help me with this ?
Reorder your rules like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^profile/(.*)$ index.php?user=$1 [QSA,L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?cod=$1 [L,QSA]
</IfModule>
I am using the following code to rewrite a url on my website:
RewriteEngine On
RewriteBase /
RewriteRule ^about/organisation$ pages/about/organisation [NC,L]
This hides the "pages" bit of the url, But if the user types in "about/organisation/index.php" they still get to the page and index.php is shown. What I want to do is if they type in the index.php it redirects them to "about/organisation" effectively hiding the file name.
I have tried this but it didnt work.
RewriteRule ^about/organisation$ pages/about/organisation/index.php [NC,L]
What am I doing wrong?
EDIT: from the answers it seems that my server doesnt let me access the relevant files so Ill have to solve that one first
You can use:
RewriteEngine On
RewriteBase /
RewriteRule ^(about/organisation/).+$ $1 [R=302,L,NE,NC]
RewriteRule ^about/organisation/?$ pages/about/organisation [NC,L]
Maybe try your rule like this.
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /about/organisation/([^/]+)
RewriteRule ^ /about/organisation? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
REwriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^about/organisation/?$ pages/about/organisation [NC,L]
My current .htaccess file contains the following lines:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I would like to redirect www.domain.com/category/automotive/used+car+dealers to www.domain.com/category.php?category=automotive&subcategory=used+car+dealers.
Should be dynamic and not a static redirect, as I will have other categories and subcategories? Can anyone show me some light on how to proceed from here?
I have googled, and browsed Stack Overflow for nearly 2 hours, but I'm still unable to come out with a solution.
[Sample 1]
RewriteEngine On
RewriteRule ^category/([\w-+]+)/([\w-+]+)$ category.php?category=$1&subcategory=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I have tweak abit and is able to redirect to the correct file, but somehow the page loads without css and images. please advise. thankyou.
[Sample 2]
RewriteEngine On
RewriteBase /websitefolder/
RewriteRule ^category/([\w-+]+)/([\w-+]+)$ category.php?category=$1&subcategory=$2 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This works fine just that the url will become category.php?category=automotive&subcategory=used+car+dealers instead of /category/automotive/used+car+dealers. but i wish to have the latter. please advise
Try this:
RewriteEngine On
RewriteBase /
RewriteRule ^(category)/([\w-+]+)$ $1.php?$1=$2 [R,L]
RewriteRule ^(category)/([\w-+]+)/([\w-+]+)$ $1.php?$1=$2&subcategory=$3 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteEngine On
RewriteBase /websitefolder/
RewriteRule ^category/([\w-+]+)/([\w-+]+)$ category.php?category=$1&subcategory=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Just remove the R flag from :
RewriteRule ^category/([\w-+]+)/([\w-+]+)$ category.php?category=$1&subcategory=$2 [R,L]
It will not change the URL in the browser.
Here's what should fit your exactly your need:
RewriteEngine On
RewriteBase /
RewriteRule ^category/([a-zA-Z0-9]+)(/([a-zA-Z0-9]+))?$ category.php?category=$1&subcategory=$3 [QSA,NC,L]
Now if you want something more generic, i.e.:
www.domain.com/category/automotive/used+car+dealers
=>
www.domain.com/category.php?category=automotive&subcategory=used+car+dealers
and
www.domain.com/title/automotive/used+car+dealers
=>
www.domain.com/title.php?title=automotive&subcategory=used+car+dealers
Please see ThinkingMonkey's answer it's better than mine :).