url rewriting not working automatically - apache

I have problem with url rewriting.
I want to change dynamic link example.com/new.php?name=latest-news to example.com/latest-news-of-the-day my .htaccess file configuration is
RewriteEngine on
RewriteRule (.*)-of-the-day$ new.php?name=$1[NC,L]
Now the page url is still the same, but if I write manually example.com/latest-news-of-the-day it works. I want to display new url according to .htaccess file but it is not showing automatically.

You need to add another rule to externally redirect the browser when they request the new.php page:
RewriteCond %{THE_REQUEST} \ /+new\.php\?name=([^&\ ]+)
RewriteRule ^ /%1-of-the-day? [L,R=301]

Your incoming URI is /<something>-of-the-day, and you want to "invisibly" change it to the real file /new.php?name=<something>?
RewriteEngine On
RewriteRule ^(.*)-of-the-day/?$ /new.php?name=$1 [NC,QSA]
The [L] flag is unnecessary if there is no RewriteCond, and the [QSA] flag preserves any Query String that came in with the original URI. Note that it does not check if the original URI was an existing directory or file (you can add RewriteCond statements to restrict that).

Related

Dynamically hide all sub folder but initial

Essentially, I need to edit the .htaccess file to develop a way to hide folder structure after /file. For example, if I have this URL:
https://www.example.com/file/page/work/assignments.php?
I should only see https://www.example.com/file/assignments.php?
How can this be accomplished?
So far I have tried the following, but it does hide all folders after first:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule !^subfolder/ /subfolder%{REQUEST_URI} [L]
You can do something like the following to "hide" page/work after the /file/ directory and before the last path segment.
This needs to go in the root .htaccess file.
RewriteEngine On
# Redirect "/folder/page/work/<file>" to "/folder/<file>"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(folder)/page/work/([^/]+)$ /$1/$2 [R=302,L]
# Rewrite "/folder/<file>" back to "/folder/page/work/<file>" (if it exists)
RewriteCond %{DOCUMENT_ROOT}/$1/page/work/$2 -f
RewriteRule ^(folder)/([^/]+)$ $1/page/work/$2 [L]
The will externally redirect a request for /folder/page/work/assignments.php?something to /folder/assignments.php?something. And internally rewrite the request back again.
NB: You should already be linking to /folder/<file> internally. The initial redirect to remove page/work is just for SEO if you are changing an existing URL structure. Otherwise, this is not strictly necessary.
A look at your existing rule...
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule !^subfolder/ /subfolder%{REQUEST_URI} [L]
This would seem to be unrelated to what you are trying to achieve. It would internally rewrite a request for /<anything> to /subfolder/<anything>, essentially "hiding" /subfolder.
There is no need to check the requested Host header unless you are accepting requests to multiple domains and this is specific to just one of those domains. (You've made no mention of this in your question.)

htaccess only one file in subfolder and keeping the url

I need to put files in a new folder like this:
domain.com/newsite/newpanel/events.php
There are other files in the server like:
domain.com/events.php,
domain.com/oldsite/events.php etc.
Requirement 1:
I cannot redirect everything (because old implementations exist) with generic rules,
so I only want to redirect specific urls.
domain.com/events should now skip the old files and go to domain.com/newsite/newpanel/events.php
Requirement 2:
I tried something like this
RewriteRule ^events /newsite/newpanel/events/$1 [P]
but the url on the url bar will change. Is it possible for it to display domain.com/events?
thank you all!
EDIT: Since OP has mentioned different URLs in comments section so adding solution as per that here.
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/CProjects/events/?$ [NC]
RewriteRule ^(.*)$ CProjects/folderA/folderB/events [L]
Based on your shown samples, could you please try following. Please make sure you clear your browser cache before testing your URLs after putting these Rules into your .htaccess file.
RewriteEngine ON
RewriteCond ^/events/?$ [NC]
RewriteRule ^(.*)$ newsite/newpanel/events.php [L]

Editing .htaccess file to modify URL

I'm trying to modify my .htaccess file to modify my URL and have tried many methods but cannot achieve exactly what I want. For example I have this URL:
http://mywebsite.com/FOLDER/index.php?id=5
Now I want it to look like:
http://mywebsite.com/FOLDER/5
or
http://mywebsite.com/FOLDER/ID/5
My .htaccess contains the following code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^index/([0-9]+)/([0-9a-zA-Z_-]+) index.php?id=$1 [NC]
I cannot figure out what's wrong. Thanks.
You can use:
RewriteEngine on
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+FOLDER/index\.php\?id=(\d+) [NC]
RewriteRule ^ /FOLDER/%1? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^FOLDER/(\d+)/?$ FOLDER/index.php?id=$1 [L,QSA,NC]
The first argument of RewriteRule is what the incoming url without domain and without preceding paths (more on that later) is going to be matched against. This url is, in your case, http://mywebsite.com/FOLDER/5. Assuming that your .htaccess file is in your DocumentRoot, the regex will match against FOLDER/5.
You are currently trying to match FOLDER/5 with ^index/([0-9]+)/([0-9a-zA-Z_-]+), which is not going to work. A better regex would be ^(.*)/([0-9]+)$ or ^(.*)/ID/([0-9]+)$. You can then rewrite to $1/index.php?id=$2. I would recommend using the [L] flag to stop rewriting for this round to avoid common problems with multiple rules matching while you do not expect them to.
Besides this, make sure that your .htaccess files are being read (e.g. by checking that if you enter garbage, you get a 500 internal server error), that mod_rewrite is enabled, that you are allowed to override FileInfo. You also may need to turn AcceptPathInfo off.

.htaccess and rewrite from subdirectories inside unknown parent directory

So I have an htaccess file in a subdirectory and whenever I try to rewrite the url, it redirects to the document_root and not the subdirectory where the htaccess resides. Now, under normal circumstances, I'd rewrite it with the path to the subdirectory with path/to/subdirectory, but I won't know what the exact path will be. Is there a way either, through an Apache environment variable or something else, to write out that path?
Edit:
Here's the .htaccess file so far.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule (.*-file) a/b/c/$1.file
RewriteRule (.*-file) $1.file
So, I'm trying to, if the request contains the word file, I want to match the entire request prior to the word file and redirect there. This is so that if a request is to
example.com/a/b/c/file[any characters here].file
the request will be redirected to the right file. To reiterate, the problem is that I am trying to redirect within the subdirectory. So when I say Rewrite $1, I want that to include the entire request and not just what matched in the REQUEST_FILENAME. And the reason I need it to do that is because I can't simply put a/b/c/$1.file since I won't know for absolute certainty the a/b/c part.
Edit 2: Examples:
So, an example is that I'd send a request like:
example.com/a/b/c/fileacs.file
And want to redirect to:
example.com/a/b/c/file.file
Where I do not know a/b/c/. I have an actual regex and set of rules for the real-world use of this redirect, so don't mind the ridiculous nature of this example.
But currently it's redirecting to:
example.com/file.file
Which does not exist and even if it did, I do not want to redirect there. I've read about Rewrite Context, but can't find out anything substantial about it nor if it's the cause for this. Thank you, in advance.
You can use this rule to capture any path before fileacs.file and use that as bach-reference in RewriteRule:
RewriteEngine On
RewriteCond ^(.*)/file[^.]+\.file$ [NC]
RewriteRule ^ %1/file.file [L,R=302]
The solution is to use the a RewriteCond on the %{REQUEST_URI} (thanks anubhava!) that checks matches the entire request except for the %{REQUEST_FILENAME} without capturing it, using a lookahead. Write out this with the %1 followed by the desired filename. See the following:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} (.*)-file(?!\.file) [NC]
RewriteRule ^ %1.file [L,R=301]
The %1 now holds the path up to the directory that the .htaccess is stored, plus any prefix to the filename. This doesn't match the remainder of the request, but rather looksahead to ensure you're not actually requesting the file you would like to redirect to (causing a loop).

Apache mod_rewrite redirect, keep sub-directory name

I'm wondering if this is possible.
I have a single page site in which I'd like to incorporate a trailing slash with a file name that anchors to a section on that site. I'm trying to avoid using hash or hash-bangs.
For example; www.example.com/recent
Right now, I'm removing any trailing slash, but I get a 404 with /recent because it's expecting a file.
RewriteRule ^(.*)/$ /$1 [R=301,L]
Is it possible to redirect to www.example.com, but still maintain the /recent without the server thinking it's a file so I can read it client-side (php/js)? More so that I can keep using the back and forward buttons.
Thanks for any help!
TBH it is not 100% clear for me what you want. As I understand you want URL www.example.com/recent to be rewritten (internal redirect, when URL remains unchanged in browser) to www.example.com/index.php?page=recent (or something like that).
DirectorySlash Off
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# remove trailing slash if present
RewriteRule ^(.*)/$ /$1 [R=301,L]
# do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
# rewrite all non-existing resources to index.php
RewriteRule ^(.+)$ /index.php?page=$1 [L,QSA]
With the above rules (that need to be placed in .htaccess in website root folder) this can be achieved. Request for www.example.com/recent will be rewritten to www.example.com/index.php?page=recent so your single-page server side script knows which URL was requested. The same will be with any other non-existing resource e.g. www.example.com/hello/pink/kitten => www.example.com/index.php?page=hello/pink/kitten.
It may not be necessary to pass originally requested URI as a page parameter as you should be able to access it in PHP via $_SERVER['REQUEST_URI'] anyway.
If I misunderstood you and this is not what you want then you have to clarify your question (update it with more details, make it sound clear).