Rewriterule - urls dont change in the browser bar - php

It's very strange to me, but:
I have a website with guestbook-add.php. I want to show it to the visitors as gasterbuch.html.
So I have written in .htaccess (my real domain has been changed in that listing to http://mywebsite.com/ of course):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.html$ http://mywebsite.com/index.php [L]
RewriteRule ^gasterbuch.html$ http://mywebsite.com/guestbook-list.php
RewriteRule ^eintrag-hinzufugen.html$ http://mywebsite.com/guestbook-add.php
</IfModule>
Now, when I type in the browser http://mywebsite.com/gasterbuch.html, I see the text generated by guestbook-add.php (which is fine).
But in the browser url bar, I see http://mywebsite.com/guestbook-add.php instead of http://mywebsite.com/gasterbuch.html (like it was the 302 redirect).
What am I doing wrong?

Changing
RewriteRule ^index.html$ http://mywebsite.com/index.php [L]
RewriteRule ^gasterbuch.html$ http://mywebsite.com/guestbook-list.php
RewriteRule ^eintrag-hinzufugen.html$ http://mywebsite.com/guestbook-add.php
to
RewriteRule ^index.html$ /index.php [L]
RewriteRule ^gasterbuch.html$ /guestbook-list.php [L]
RewriteRule ^eintrag-hinzufugen.html$ /guestbook-add.php [L]
should work.

Related

Htaccess redirect seems not working with php file extension to seo friendly (no extension) url

One of my website's old URL structures is like this:
https://example.com/login.php
or,
https://example.com/login.php?redirect=https://example.com
or,
https://example.com/register.php
after rebuilding the URLs, the URLs look like this:
https://example.com/login
or,
https://example.com/login?redirect=https://example.com
or,
https://example.com/register
Now when the user/visitor uses the old URL https://example.com/login.php, I want to redirect to the https://example.com/login page. How can I do it?
FYI, currently, my .htaccess file has the following content:
AddDefaultCharset UTF-8
ErrorDocument 404 /error-404/
##
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
## Allow a few SEO Files direct access.
RewriteRule ^robots.txt?$ robots.txt [L]
RewriteRule ^ads.txt?$ ads.txt [L]
RewriteRule ^sellers.json?$ sellers.json [L]
## Avoid rewriting rules for the admin section
RewriteRule ^(admin|resources)($|/) - [L]
## Set Ajax Request File
RewriteRule ^kahuk-ajax.php?$ kahuk-ajax.php? [L,QSA]
## Set controller with id
RewriteRule ^([^/]+)/([0-9]+)/?$ index.php?con=$1&id=$2 [L,QSA]
## Set controller with slug
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?con=$1&slug=$2 [L,QSA]
## For paging
RewriteRule ^([^/]+)/page/([0-9]+)/?$ index.php?con=$1&page=$2 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/page/([0-9]+)/?$ index.php?con=$1&slug=$2&page=$3 [L,QSA]
## Set controller for only one parameter
RewriteRule ^page/([^/]+)/?$ index.php?con=page&slug=$1 [L,QSA]
RewriteRule ^([^/]+)/?$ index.php?con=$1 [L,QSA]
## Set home page
RewriteRule ^/?$ index.php?con=home [L]
You can insert this rule just below RewriteBase / line to remove .php extension:
# To externally redirect /afile.php to /afile
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule !^admin/ /%1 [R=301,NE,L,NC]

htaccess url-rewriter page not found

i work in localhost and i need to rewrite url, when i write the url rewrited, a page like : 404 Not found This page not found on this server ...
But the directory is good
I already try to uncomment the line for mod_rewrite in httpdconf.conf
but not work
# URL REWRITING
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^esthetique$ /view/frontend/aestheticView.php [L]
RewriteRule ^forfait-fiancee-normal$ /view/frontend/forfaitFianceeBasicView.php [L]
RewriteRule ^forfait-mariee-normal$ /view/frontend/forfaitMarieeBasicView.php [L]
RewriteRule ^forfait-mariee-belle-dun-soir$ /view/frontend/forfaitMarieePremiumView.php [L]
RewriteRule ^forfaits$ /view/frontend/forfaitsView.php [L]
RewriteRule ^coiffure$ /view/frontend/hairView.php [L]
RewriteRule ^accueil$ /view/frontend/homeView.php [L]
RewriteRule ^mentions-legales$ /view/frontend/legalsView.php [L]
RewriteRule ^maquillage$ /view/frontend/makeupView.php [L]
RewriteRule ^packs-reductions$ /view/frontend/packsView.php [L]
RewriteRule ^politique$ /view/frontend/politiqueView.php [L]
When i go to the url mywebsite.ex/esthetique
He tell me :The requested URL /view/frontend/aestheticView.php was not found on this server.
And he work's when is online

htaccess cant get rewrite seo friendly url

hi i have use many different htaccess codes, but i cant get rewrite to work.
i want this url
https://domain.com/category.php?cat=firm
to look like this url
https://domain.com/category/firm
this is my latest attempt
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^/]*)\.html$ /category.php?cat=$1 [L]
ErrorDocument 404 https://seoboost.no/404page.php
RewriteCond %{HTTP_HOST} ^www\.seoboost\.no$
RewriteRule ^/?$ "https\:\/\/seoboost\.no\/" [R=301]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "https\:\/\/example\.com\/" [R=301,L]
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F]
RewriteRule ^index\.php$ / [R=301]
RewriteRule ^(.*)/index\.php$ /$1/ [R=301]
i have try to delete all in my htaccess and only have this code
RewriteEngine on
RewriteRule ^([^/]*)\.html$ /category.php?cat=$1 [L]
but it still not working, is my server or what am i doing wrong??
Try replacing your .htaccess with
RewriteEngine On
RewriteRule ^category/(.*)$ /category.php?cat=$1 [L]
which will redirect yoursite.com/category/12 to yoursite.com/category.php?cat=12 internally, and the user will never see the "ugly" url. Then inside your category.php file you can access the cat by $category_id = $_GET['cat']
A simple anchor tag looks like this:
Item 12
Pulling from Justin Lurman's answer here, the following should work:
RewriteEngine On
# redirect "/category.php?cat=..." to "/category/..." to hide a directly accessed "ugly" url
RewriteCond %{THE_REQUEST} \s/category\.php\?cat=(.+)\s [NC]
RewriteRule ^ /category/%1? [R=301,L]
# internally rewrite "/category/..." to "/category.php?cat=..."
RewriteRule ^category/(.+)$ /category.php?id=$1 [L]
As Justin noted in that answer to a similar question, you need to confirm that htaccess files are enabled/allowed in your Apache configuration and that mod_rewrite is enabled.
Additionally, I am sure you are aware, but just in case you are not, after implementing this new htaccess redirect rule, you should change all of the links on your webpages to use clean/friendly URLs. By doing so, neither your users nor a search engine crawler will access the "ugly" URLs unless they access them directly (via a bookmark or a saved link).

Rules conflicts in htaccess

I am working with my class on a project with an htaccess file that doesn't seem to work. I have this:
SetEnvIf Request_URI ^ root=/myroot/
RewriteRule ^(assets|inc) - [L]
RewriteRule ^section[/]?$ %{ENV:root}section/index
RewriteRule ^section/function/(.*)$ %{ENV:root}index.php?type=section&action=function [L]
RewriteCond %{QUERY_STRING} ^(.*)?$
RewriteRule ^section/(.*)$ %{ENV:root}index.php?type=section&action=view [L]
RewriteRule ^index\.php$ - [L]
In fact, there is three things I don't understand:
If I query an url as /section/function/, I should get /myroot/index.php?type=section&action=function but I get localhost/section/function in my browser, leading to an error. Are the url translated bidirectionnaly : myroot/index.php?type=section&action=function appearing in the browser as /section/function/. If I delete the last line , I get blank error page, Why so ?
Edit :
So conformally to what Olaf Dietsche said, I changed the rules into
SetEnvIf Request_URI ^ root=/myroot/
RewriteRule ^(assets|inc) - [L]
RewriteRule ^section[/]?$ %{ENV:root}section/index
RewriteRule ^section/function/(.*)$ %{ENV:root}?type=section&action=function [L]
RewriteCond %{QUERY_STRING} ^(.*)?$
RewriteRule ^section/(.*)$ %{ENV:root}?type=section&action=view [L]
RewriteRule ^index\.php$ - [L]
But when I click in , I am shown a page localhost/section/some function. I really don't know what is going wrong....
First, you must have
RewriteEngine on
in the .htaccess for the rules to work.
The rules rewrite the requests, but don't redirect the client. The difference between the two is
rewrite send another page's content to the client, without telling the client. The client thinks, that it got the contents of the requested page.
redirect send another URL to the client, causing the client to make a new request with this URL. The client knows (and displays in the browser's bar), that it received the contents of another page.
If you want to redirect the client, you must add an R flag to the RewriteRule
RewriteRule ^section/function/(.*)$ %{ENV:root}index.php?type=section&action=function [R,L]
Update:
You must remove %{ENV:root} in front of the substitutions, because the URL is e.g. /index.php and not /myroot/index.php
RewriteRule ^section[/]?$ /section/index
RewriteRule ^section/function/(.*)$ /index.php?type=section&action=function [L]
RewriteCond %{QUERY_STRING} ^(.*)?$
RewriteRule ^section/(.*)$ /index.php?type=section&action=view [L]
RewriteRule ^index\.php$ - [L]

Issue with mod_rewrite when accessing directory

My code:
RewriteEngine on
RewriteBase /
RewriteRule ^article/([a-z]+)/?$ index.php?page=article&name=$1 [L]
RewriteRule ^([a-z]+)/?$ index.php?page=$1 [L]
I am using WAMP and had setup a Virtual Host.
In my index.php, there is code to get page passed and checks if it exists(in database). If not, display an error message. It works fine.
Eg: http://mysite/contactus/
But it will not work if I use a a directory name as page_name in the URL. Eg: http://mysite/images/. This will display page not found error (ie. checks database and no page found, so display "not found"). But it will not display images,css(linked file) in the page. Also, it shows http://mysite/images/?page=images in addressbar.
Like that, if I goto js folder which is used to store javascript files, above problem occurs. So, problem is caused if any subdirectory's name is passed as pagename.
How to solve this ?
When http://mysite/images/ is supplied, mod_rewrite is redirecting to http://mysite/images/index.php?page=images instead of http://mysite/index.php?page=images
Edit
Please tell me how to block hotlinking of files and directory, and redirect back to index page or send some browser header error ?
I tried this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*) http://%{HTTP_HOST} [R,L]
RewriteRule ^article/([a-z]+)/?$ /index.php?page=article&name=$1 [L]
RewriteRule ^([a-z]+)/?$ /index.php?page=$1 [L]
Edit
New code(semi-working):
RewriteEngine on
RewriteBase /
# remove trailing slash ONLY if it is not an existing folder
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1/ [R,L]
RewriteRule ^article/([a-z]+)/?$ http://%{HTTP_HOST}/index.php?page=article&name=$1 [L]
RewriteRule ^([a-z]+)/?$ http://%{HTTP_HOST}/index.php?page=$1 [L]
This code will clear the problem with not displaying pics and css when a directory name is mentioned. But whatever pagename i specify eg:http://mysite/contactus, it will goto URL: http://mysite/index.php?page=contactus. Even if I use a directory name eg: http://mysite/js, it will goto: http://mysite/index.php?page=js
I am very confused.
RewriteEngine on
RewriteBase /
RewriteRule ^article/([a-z]+)/?$ index.php?page=article&name=$1 [L]
RewriteRule ^([a-z]+)/*$ /index.php?page=$1 [L]
you have to put the slash in front.
Edit: changed the ? to *
My understanding is that your script is for documents only, not images or other resources.
Then you should ignore them right away. Try adding this line right after RewriteBase like this :
RewriteEngine on
RewriteBase /rewrite/
RewriteRule ^/(images|js)/(.*)$ - [L]
RewriteRule ^article/([a-z]+)/?$ index.php?page=article&name=$1 [L]
RewriteRule ^([a-z]+)/?$ index.php?page=$1 [L]
Then these subdirectories would be served right away, thus bypassing the next RewriteRule set.
For the problem with the directories I usually force a slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+[^/])$ $1/ [R]

Categories