Basically I'm trying to convert my query string urls to a directory structure for SEO purposes. And I have been getting to the point where im trying to say the same logic in many semantical ways in the .htaccess file to see if I can get a result. Here is what I'm looking to do.
The request is:
mywebsite.com/test/antitrust/somelongfilename/
Then modified to request:
mywebsite.com/test/antitrust/?id=news&post=news/somelongfilename.html
So that when people click on my SEO friendly links they get changed to the ugly ones to which my php code can read. "somelongfilename.html" is not to be shown by itself, its just content thats pushed to a content area of a php file.
FYI, post=news/somelongfilename.html is actually the directory/file to goto from the antitrust directory, so its located /test/antitrust/news. Its been working fine even though its probably not a good idea to put / in a query string.
The .htaccess is in the subdirectory /antitrust, as I don't want to touch the root directory .htaccess as its servicing many websites that I dont want to affect since im not a .htaccess expert.
RewriteOptions inherit
RewriteEngine On
RewriteCond %{HTTP_HOST} news
RewriteRule ^/(.*) index.php/?id=news&post=$1 [QSA,L]
Also tried many variations of this:
RewriteEngine On
RewriteOptions inherit
RewriteCond %{HTTP_HOST} ^news$
RewriteRule \/(.+?)\/? ^?id=news&post=$2 [QSA,L]
RewriteRule ?id=(.+?)&post=(.+?) http://mywebsite.com/test/crush/antitrust/$1/$2/ [QSA,R]
Any ideas on what part im doing wrong? Thanks.
Try:
RewriteEngine On
RewriteOptions inherit
RewriteBase /test/antitrust/
# not sure why you need this, it's not part of your question
RewriteCond %{HTTP_HOST} news
# conditions to prevent looping
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)/?$ index.php?id=news&post=$1 [QSA,L]
Related
I'd like to ask if it's possible to use a dynamic url and GET at the same time.
Let's say my current dynamic url is: https://yourdomain.com/blog/this-is-a-title
Would it be possible to make this work too: https://yourdomain.com/blog/this-is-a-title?action=delete
RewriteEngine on
RewriteRule ^([0-9]+)$ index.php?id=$1
The dynamic url mentioned first works fine, but I want to make the second work as well.
This is my .htaccess - hope it helps.
PS: I know that the regex in my htaccess isn't correct, it's just an example.
Have your .htaccess Rules file in following manner. Please make sure that your htaccess Rules file is present in root folder(where blog and htaccess both are residing in it; htaccess shiouldn't be inside blog folder; should be place same folder with it). Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Newly added rules here...
RewriteBase /blog/
RewriteCond %{THE_REQUEST} \s/blog/(?:[^?]*)?action=(\S+)\s [NC]
RewriteRule ^ index.php?action=%1 [L]
##Old Rules OP's htaccess ones.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)$ index.php?id=$1
The website I'm working on is using some cms. I need to add a static website to this. When I put mypage.html in the main directory and go to www.website.com/mypage.html it works. I would like the page to be accessible without '.html' ending. I experimented with editing htaccess files but always end up with error of too many redirections.
What I entered were various combinations, for example
Redirect 301 http://website.com/mypage http://website.com/mypage.html
The htaccess file I'm using looks like this:
:Location /*.php
Use php54
:Location
RewriteEngine On
DirectoryIndex index_prod.php
Options -Indexes
RewriteRule ^.*\.(css|png|swf|js|gif|jpeg|jpg|flv|pdf|doc)$ - [L]
RewriteRule ^net2ftp - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^/?$ plug.html [L]
#RewriteCond %{REQUEST_URI} !=/
RewriteRule ^/?.* index_prod.php
I'm looking for tips or to be explicitly told what and where to put in htaccess file to make it work (if it's possible)
Could you please try following, considering that you want without extension file URLs to be served by html extension files. Also since you didn't mention any specific condition before RewriteRule hence that redirection errors are coming to it, because its keep on redirecting in lack of any condition/check's presence(till its maximum redirection limit is crossed).
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ /$1.html [NC,L]
I am trying to get a pretty permalink system set up on a site I've just built and nothing is having any effect. I either get a 500 error or the permalinks don't work and/or it breaks all the links to CSS and images while still giving a 404 if you try to use the permalink structure.
Here is my .htaccess code:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /main/index.php?act=$1&sub=$2&id=$3 [L]
I want it to convert links like index.php?act=admin&sub=edit-product&id=5 to /admin/edit-product/5 whilst including any in between. For example, if a URL has an ID call but not a sub call it will still work.
Any help would be greatly appreciated.
UPDATE:
I have added RewriteBase / and it has now doing something, but the permalinks are not working properly.
Is there a way I can find out where the rewritten URL is actually pointing?
It seems that full URLs that use all the rewrite rules work, but not the URLs that only need some of them.
I use this .htaccess code for rewrite in my projects:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
This code pass exists request (for static files and etc) as is, but all other redirect to index.php. Request uri available int PHP as
$_REQUEST['REQUEST_URI']
In PHP we (or our framework) can parse it as we will.
On the start i want you to take notice, i did try finding solution in google, or in stackoverflow. I found some tips, but they didn't work for me, or i simply failed following instructions.
So, here is my site directories:
www.domain.pl/index.php <--index file
www.domain.pl/images/ <--images folder
www.domain.pl/styles/ <--styles folder, for now just 1 css file
www.domain.pl/script/ <--scripts folder, js files, and 1 php file called with include
www.domain.pl/font/ <--font files
Now, when i open my website with www.domain.pl or www.domain.pl/index.php it works like a charm. But i want to be able to add some parameter for example www.domain.pl/index.php?action=dosmth, but it doesn't look good for a users, so thats the place where I wanted to use mod rewrite.
Now comes tricky part, i want it to look like www.domain.pl/index/actionparam/ but when i do that, I get error in console, saying my images, scripts, and css cant be loaded/found. (i tried rewriting it like that: RewriteRule ^index/([^-]+)/script/jquery-1.7.1.min.js$ script/jquery-1.7.1.min.js [L] but I cant use it for every single image, script, css, or font file. There need to be better way.
So, my mod rewrite so far:
Options FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.pl/$1/ [L,R=301]
RewriteRule ^index.html$ index.php [L]
RewriteRule ^index/$ index.php [L]
RewriteRule ^index/([^-]+)/$ index.php?action=$1 [L]
First part, i am trying to add "slash" at the end of url, if user didnt put it there.
http://domain.pl/index->http://domain.pl/index/ or
http://domain.pl/index/myaction->http://domain.pl/index/myaction/
Question
Is it possible to somehow skip folders with images, scripts etc when rewriting it? Or how could i rewrite all images at once?
Additional question
http://domain.pl/myaction/ redirect straight to http://domain.pl/index.php?action=myaction right away, without need of putting /index/ in there? Yes, i am learning mod rewrite.
You don't have to include index in the request, only the parameter. Example:
Request: http://domain.pl/myaction
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
# Exclude existing files and directories.
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
# Get the parameter and pass it to index.php
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteRule ^([^/]+)/? /index.php?action=$1 [L,NC]
So I'm building an ecommerce website using a file structure suggested to me, it looks like this:
http://www.sample.com/index.php
http://www.sample.com/department/index.php
http://www.sample.com/department/men/index.php
Basically I want to get rid of all the index.php filenames in the url, so it looks like this:
http://www.sample.com
http://www.sample.com/department
http://www.sample.com/department/men
I've achieved this for my homepage with adding this code in .htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
But, is there any way to apply this rule to every subdirectory in my site with just editing the root .htaccess file? I'm guessing it can be done by adding this rule in .htaccess files in every directory but I'd rather just have one.
Also, is this even a good way to structure a website? I am not particularly fond of it as there are so many index.php files but I'm not sure of a better alternative...
Try putting these rules above the rules that you have in your htaccess file in the document root:
RewriteRule ^index.php$ / [L,R=301]
RewriteRule ^(.+)/index.php$ /$1/ [L,R=301]