rewrite rule is not redirecting properly - .htaccess

I would like to be able to use rewrite rules to redirect anyone who opens the link :
domain.com/name
to
index.php?username=name
what would be the best way to do it?
I tried to post the htaccess code I wrote but stackoverflow keeps saying it doesn't meet standards so I removed it.
thanks in advance

Something like this will do it:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([^/]+)/? [NC]
RewriteRule .* index.php?username=%1 [L,QSA]
It will map silently
http://domain.com/name
To
http://domain.com/index.php?username=name
For this to work, /name must be the first directory in the incoming URL path.

Related

URL rewriting not working from index.php?id=2 to /2

I have a URL called
http://localhost:8080/text/index.php?id=2
and I have to redirect on
http://localhost:8080/text/2
So I added the below code in the .htaccess but it's not working
RewriteEngine On
RewriteRule ^([^/d]+)/?$ index.php?id=$1 [QSA]
I refer to the below two links. Is there any issue with my code?
common-htaccess-redirects-19-6-2018 and htaccess-rules
After suggested answer, I tried below code
HTML
Register
login
or
Register
login
.htaccess code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[\w-]+/(\d+)/?$ index.php?id=$1 [QSA,L]
With your shown samples, please try following. Please make sure to clear your browser cache before testing your URLs. This considers that you are
hitting URL http://localhost:8080/text/2 in browser.
##Making RewriteEngine ON here.
RewriteEngine ON
##Placing conditions to check if these are non-existing pages only.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
##Writing rule to rewrite to index.php with needed variable here.
RewriteRule ^[\w-]+/(\d+)/?$ index.php?id=$1 [QSA,L]
Issues in OP's attempt: You are trying to attempt to match digits in starting where your url doesn't have digits, so rather use [\w-]+ with it. Also use QSA,L flags with your rewrite rule to handle query string and come out of the rule in case this is executing.

rewriting url with .htacces cause files not to be found

In general, I am trying to understand how .htaccess works. I would highly appreciate it if anyone points me in the right direction. I have been trying to make the following url (with optional parameters) pretty.
mysite/v1.0/foldername
mysite/v1.0/foldername/param1/
mysite/v1.0/foldername/param1/param2/etc
I tried the following:
RewriteEngine On
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$ foldername.php [QSA,L]
the problem is that when I get it to pass the parameters it can no longer get the resources. It seems to have changed directory.
.htaccess is in foldername
Also, I would like to know what site i can go to to learn about REQUEST_URI, REQUEST_FILENAME, etc. A site that is not too technical as it's the apache site.
You are incorrectly rewriting the rules correct rule according to your need would be like,
RewriteEngine On
# rule for removing extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([\w-]+)/?$ $1.php [QSA,L]
# below cond means incoming url is nor a file neither a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# actual rules
RewriteRule ^([\w-]+)/([\w-]+)/?$ $1.php?param1=$1 [L]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ $1.php?param1=$2&param2=$3 [L]
Refrences
Reference: mod_rewrite, URL rewriting and "pretty links" explained
http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Give each user a custom page with htaccess

I was wondering how I could give each user a custom page like on YouTube where they have YouTube.com/users/Username
My approach would be to modify an .htaccess file so if I had www.domain.com/cgi-bin/user.py?username=x, and someone wrote in the url www.domain.com/users/x, it would direct them to that page above. I'm just not sure exactly how to write an .htaccess to meet that criteria.
Any help would be great.
Thanks!
The code below should work:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /cgi-bin/user\.py\?username=(.*)\ HTTP
RewriteRule ^ /users/%2\? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/users/(.*)\$ /cgi-bin/user.py?username=$1 [L]

mod-rewrite forwarding without changing URL

I have a small problem with my Apache configuration when creating "pretty" URLs. I've got it to the stage where typing (or linkig for that matter) to
index.html
forwards you to
index.php?pageID=Forside
that is exactly what I want.
But how can I get index.html to stay in the address bar of the browser?
Right now it forwards and changes the URL to the original one.
Here my .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} index\.html
RewriteRule .* http://www.radoor-designs.dk/index.php?pageID=Forside [L]
And before someone comments on it: Options +FollowSymLinks is missing since it triggers an error 500 on a one.com webhotel.
Thanks in advance!
Try the following:
RewriteEngine On
RewriteRule ^index\.html$ /index.php?pageID=Forside [L]
I think this may help you to resolve your problem.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index.html$ /index.php?pageID=Forside [L]
This will do the redirect for you whilst showing index.html in the browser window.
Strange that symbolic links creates an error 500,
if you want it to redirect to index.html?pageID=Forside then do
RewriteRule .* /index.html?pageID=Forside [QSA,L,R=301]
I'm not 100% certain what you are trying to achieve with this could you explain a little more?

using - instead of _ (underscore) with .htaccess

I'm using with good results the following code to access alla of my php files into the /it directory without specifying the extension. In other words I can access to "http://www.mydomain.com/it/about.php" just writing "http://www.mydomain.com/it/about".
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://www.mydomain.com/it/$1.php [L]
the same happen when i try to access to http://www.mydomain.com/it/question_answers.php.
How can I access directly to *"http://www.mydomain.com/it/question_answers.php"* also writing "http://www.mydomain.com/it/question-answers"?
I wrote the floowing code below the previous but it seems not to work.
Redirect 301 /question-answer http://www.mydomain.com/it/question_answer.php
because if i write "http://www.mydomain.com/it/question-answer" the browser try to open the page:
"http://www.mydomain.com/it/question-answer.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php"
A small abstract of the post:
I have the page *"http://www.mydomain.com/it/question_answers.php"*
with the first part of code I can get it using the link *"http://www.mydomain.com/it/question_answers"*
I'd like to access the same page also with the following "http://www.mydomain.com/it/question-answers"
Thanks!
This should work for you:
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://www.mydomain.com/$1.php
RewriteRule it/question-answer\.php http://www.mydomain.com/it/question_answer.php [R=301,L]
I have changed two things: I deleted it/ in the new URL of the first RewriteRule. Otherwise you would be redirected to it/it/
I also added \.php to the second RewriteRule. I don't really know why, but the RewriteRule seems to replace the pattern instead of redirecting. And if your pattern is it/question-answer and the real url is it/questions-answer.php the .php will not be replaced.

Resources