I have links to my website like:
http://codecordia.com/codeframe/view/page/blog?post=name-of-the-article
or
http://codecordia.com/codeframe/view/page/product?post=name-of-the-product
I want to change to:
http://codecordia.com/blog/name-of-the-article
or
http://codecordia.com/product/name-of-the-product
I currently have made a .htaccess file like this, but I can't shorten the URL.
--------htaccess-------------
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
--------htaccess-------------
For your blog URL, you can use the following rule to convert your clean url to a .php file with a query string:
# URL
# INPUT http://codecordia.com/blog/name-of-the-article
# OUTPUT http://codecordia.com/codeframe/view/page/blog?post=name-of-the-article
RewriteEngine On
RewriteRule ^blog\/(.*?) /codeframe/view/page/blog?post=$1 [L]
View on htaccess.madewithlove.be
You should be able to create the second rule easily with this example.
Related
I want that if a user types in www.example.com/article-title, it gets the data from www.example.com/index.php?title=article-title. Right now it only works if a user types in www.example.com/article/article-title. I want to remove that article/.
This is what I have right now in my .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC]
RewriteRule ^article/([0-9a-zA-Z]+) index.php?title=$1 [NC,L]
If I remove article/ from the last line in my .htaccess file, it doesn't work at all.
The $1 should be article-title, but if I remove article/, $1 becomes index.
Does anyone know how I can change the .htaccess in order to let users type in www.example.com/article-title?
You just need
RewriteEngine on
RewriteRule ^(.*)$ index.php?title=$1 [NC]
tested here with
input: https://www.example.com/article-title
output: https://www.example.com/index.php?title=article-title
However i suppose this is actually what you really want, because by the looks of things you are not locking for a redirect, but something like:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
And then in the index.php file you have to "manipulate" the url requested
Hello there i have a little problem.
When using these lines of .htaccess
RewriteEngine on
Options +FollowSymLinks
# clean up file extensions .php only
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
#make get request clean
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^cms/tests/([0-9]+) cms/tests/index.php?survey=$1 [NC,L]
I transform the url
cms/tests/index.php?survey=65 to -> cms/tests/65
This works!
Now i want to have an edit mode like this:
cms/tests/65/edit or cms/tests/65/view
This is the rewrite rule I have come up with:
RewriteEngine on
Options +FollowSymLinks
# clean up file extensions .php only
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
#make get request clean
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^cms/tests/([0-9]+)/([a-zA-Z0-9_-]+) cms/tests/index.php?survey=$1&mode=$2 [NC,L]
When writing the full url like this cms/tests/index.php?survey=65&mode=edit it completely works
But it with the "clean" url it goes to my 404 page and also shows a 302 redirect in the network tab.
Is there a something that I am doing wrong or over seeing?
#make get request clean
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^cms/tests/([0-9]+)/([a-zA-Z0-9_-]+) cms/tests/index.php?survey=$1&mode=$2 [NC,L]
The second condition is never successful so the rule is not processed.
The second condition (that supposedly checks that the request + .php exists) is not what you should be doing here. If anything, you would need to check that the request does not map to a file. For example:
RewriteCond %{REQUEST_FILENAME} !-f
However, if you restrict the regex in your RewriteRule pattern by appending a $ (end-of-string anchor) then you can remove that condition altogether. For example:
#make get request clean
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^cms/tests/([0-9]+)/([a-zA-Z0-9_-]+)$ cms/tests/index.php?survey=$1&mode=$2 [NC,L]
A request that matches the regex as stated could not map to a real file (unless you have extensionless files).
Aside:
# clean up file extensions .php only
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
The second condition in your first code block is also not strictly correct and will fail under certain conditions, because the filesystem check is not necessarily the same as the file you will ultimately rewrite to, which could result in a 404 or 500 (rewrite loop) depending on your file structure. (This code block is surprisingly common, but issues relating to this keep on cropping up.)
This should really be written more like this:
# clean up file extensions .php only
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
Now, the condition %{DOCUMENT_ROOT}/$1.php matches the rewrite substitution $1.php. (Assuming the .htaccess file is in the document root.)
See my answer to the following question on ServerFault with a detailed explanation of this change: https://serverfault.com/questions/989333/using-apache-rewrite-rules-in-htaccess-to-remove-html-causing-a-500-error
my website is,
http://localhost/mywebsite/page.php?id=123
using .htaccess i changes my url like this
http://localhost/mywebsite/newpostof2016
but i want like this final url
localhost/mywebsite/newpostof2016.php
current using .htacces code is
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9]+)$ page.php?id=$1
does this work? It should add .php to all files even if they arent php files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php [L]
N.B. there are many reasons as to why you wouldn't want this behavior. I can't think of a case where this would benefit UX
This rule should do it.
RewriteEngine On
RewriteRule ^mywebsite/newpostof2016\.php$ /mywebsite/page.php?id=123 [L]
I have the following .htaccess file:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ account.php?username=$1
This basically serves my user profiles so like twitter someone can just type: url.com/theirusername
However I want to make certain pages like /home not lead to a profile but a different .php page, again without the .php at the end.
Can anyone think of a way to do this?
Thanks!
You can have another rule to add .php before this rule:
RewriteEngine On
# To internally redirect file to file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# handle profile URLs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ account.php?username=$1 [L,QSA]
just add more rules to match all the other pages, put it before the rule for usernames.
RewriteEngine On
RewriteRule ^home$ home.php
RewriteRule ^contact$ contact.php
RewriteRule ^([a-zA-Z0-9_-]+)$ account.php?username=$1
Because all my files are php I have removed php extension with this .htacess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
So I have this URL:
http://example.com/live/kanal/some-title-1
http://example.com/live/kanal/some-title-2
http://example.com/live/kanal/some-title-13
http://example.com/live/kanal/some-title-45
http://example.com/live/kanal/2some-title-333
etc ...
I want to hide kanal/ from url, to get URL like this:
http://example.com/live/some-title-1
http://example.com/live/some-title-2
http://example.com/live/some-title-13
http://example.com/live/some-title-45
http://example.com/live/2some-title-333
How can I achieve this using mod_rewrite?
I used online generators for doing the rewrite rules but none can remove a piece of text from the URL. Also I made search on this forum, and I found some posts about mod rewrite directory but I have issues when pasting in my .htacess code.
You can use this code in /live/.htaccess:
RewriteEngine On
RewriteBase /live/
RewriteRule ^(?!kanal/)(.*)$ kanal/$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Make sure there is no .htaccess in kanal/ sub-directory.