Give each user a custom page with htaccess - .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]

Related

htaccess rewrite for routing from a sub directory

I've been using this .htaccess file for enabling the grav Cms for a few sites:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/grav/
RewriteRule ^$ /grav [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /grav/$1 [L,QSA]
This works flawlessly as long the site is at the document root.
Now, I have a new site that starts in 2020/ and has Grav installed in 2020/grav.
I modified the above .htaccess to be:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/2020/grav/
RewriteRule ^2020$ /2020/grav [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^2020/(.*)$ /2020/grav/$1 [L,QSA]
This .htaccess file is located in /2020/.
While in the first case the url is left as is and Grav gets the rest of the url as parameters, in the second case (2020/) the url changes to /2020/grav, which is not what I want?
I wanted to check if it was a Grav issue and I've setup a simple test case, with the above .htaccess file and a grav/ directory with only an index.php file in there and I got the same result.
Any hint on how I have to modify the .htaccess file to get 2020/abc to stay as is in the url bar and at the same time have Grav to get the abc argument?
You can have this code in 2020/.htaccess:
RewriteEngine On
RewriteRule ^$ grav/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!grave/)(.+)$ grav/$1 [L,NC]
(?!grave/) is negative lookahead condition that will skip match if match already starts with grave/
The original question was about getting it to work with an .htaccess.
#anubhava gave a very good answer for the general case. But it sadly was not enough for Grav.
I got some more help in the Grav forums and at the end of a long journey Ole found out that the question has been asked in their forums a long time ago and a solution was found!
The /2020/.haccess can be simply defined as:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/2020/grav/
RewriteRule ^(.*)$ /2020/grav/$1
But what one need is to add a couple of configurations in the /2020/grav/user/config/system.yaml file:
custom_base_url: 'http://domain.tld/2020'
session:
path: /2020
You can find a more detailed question and Ole's reply in the Grav forums

How to use RewriteCond to get rid of duplicate page

Hello guys! I need some help with htaccess, because I'm really stuck with it. I have some pages, like:
site.ru/books
site.ru/pens
And those links can be called by other urls:
site.ru/index.php?show=books
site.ru/index.php?show=pens
I hope you understand those links above are equal. Just, urls are user friendly.
I want to use RewriteCond to make a redirect from "site.ru/index.php?show=books" to "site.ru/books", because Google or other Search Engines don't like duplicate pages.
But the problem is I don't know how to write this… Help me please, and sorry for my bad English ;-)
put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+index\.php\?show=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ index.php?show=$1 [L,QSA]

Syntax with .htaccess URL rewrite

I need to get the following URL rewrite syntax correct and am struggling.
xxxxx.com/public_html/food/
Needs to be rewritten as:
xxxxx.com/food/
I tried this and it doesn't work:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/public_html/food/(.*)$
RewriteRule ^(.*)$ http://xxxxx.com/food/%1 [R=301,L]
My client is using Joomla (which I am not familiar with), so I need to do so using the .htaccess file per everything I have researched so far. I am just struggling getting the syntax to work correctly though.
Thanks!
Try this, .htaccess
for Rewrite
RewriteEngine On
RewriteRule ^food/?(.*)$ http://xxxxx.com/public_html/food/$1 [QSA,L]
for Redirect (add R=301)
RewriteEngine On
RewriteRule ^food/?(.*)$ http://xxxxx.com/public_html/food/$1 [R=301,QSA,L]
Edit:
If you want to rewrite all urls (including /food)
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public_html/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /public_html/$1 [QSA,L]
It will allow you to access site like,
http://xxxxx.com/food/ will point to http://xxxxx.com/public_html/food/
http://xxxxx.com/sample/ will point to http://xxxxx.com/public_html/sample/
http://xxxxx.com/anything/cool/?product=123 will point to http://xxxxx.com/public_html/anything/cool/?product=123/

htaccess, rewrite multiple pages to the same page but keep original value somewhere

I have been fiddle farting around with htaccess and RewriteEngine but I can't quite get my head around it...
I'm building a website on which I want users to be able to go to /portfolio/typography for example. But I don't want to create seperate pages for each category in this portfolio and thus I want to rewrite (redirect?) all the requests that go to /portfolio/ to the index.php of this directory and load the appropiate projects for this category from there.
Any ideas on how I could do this? I used this to redirect all the requests to /portfolio/:
RewriteCond %{REQUEST_URI} !/portfolio/$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
RewriteRule .* /portfolio/ [R=302,L]
Thanks in advance,
Cas Cornelissen
EDIT
Maybe I should note that I have another .htaccess file in the root of my website.
Ok, so I found the answer to my own question...
Seems like the following is working:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1?%{QUERY_STRING} [L]

rewrite rule is not redirecting properly

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.

Resources